Commit 14aaa14f authored by Kirill Smelkov's avatar Kirill Smelkov

decoder: Preallocate .buf capacity when we know (approximate) size of output to it

This reduces allocations a bit:

    name      old time/op    new time/op    delta
    Speed-4      383ns ± 4%     378ns ± 0%    ~     (p=0.302 n=5+5)
    Decode-4    72.1µs ± 1%    70.1µs ± 0%  -2.76%  (p=0.008 n=5+5)
    Encode-4    16.5µs ± 0%    16.6µs ± 0%    ~     (p=0.095 n=5+5)

    name      old alloc/op   new alloc/op   delta
    Speed-4       280B ± 0%      280B ± 0%    ~     (all equal)
    Decode-4    35.7kB ± 0%    32.3kB ± 0%  -9.72%  (p=0.008 n=5+5)
    Encode-4    6.54kB ± 0%    6.54kB ± 0%    ~     (all equal)

    name      old allocs/op  new allocs/op  delta
    Speed-4       8.00 ± 0%      8.00 ± 0%    ~     (all equal)
    Decode-4       429 ± 0%       428 ± 0%  -0.23%  (p=0.008 n=5+5)
    Encode-4       297 ± 0%       297 ± 0%    ~     (all equal)
parent cad218c8
...@@ -536,6 +536,7 @@ func (d *Decoder) loadBinString() error { ...@@ -536,6 +536,7 @@ func (d *Decoder) loadBinString() error {
v := binary.LittleEndian.Uint32(b[:]) v := binary.LittleEndian.Uint32(b[:])
d.buf.Reset() d.buf.Reset()
d.buf.Grow(int(v))
_, err = io.CopyN(&d.buf, d.r, int64(v)) _, err = io.CopyN(&d.buf, d.r, int64(v))
if err != nil { if err != nil {
return err return err
...@@ -551,6 +552,7 @@ func (d *Decoder) loadShortBinString() error { ...@@ -551,6 +552,7 @@ func (d *Decoder) loadShortBinString() error {
} }
d.buf.Reset() d.buf.Reset()
d.buf.Grow(int(b))
_, err = io.CopyN(&d.buf, d.r, int64(b)) _, err = io.CopyN(&d.buf, d.r, int64(b))
if err != nil { if err != nil {
return err return err
...@@ -568,6 +570,7 @@ func (d *Decoder) loadUnicode() error { ...@@ -568,6 +570,7 @@ func (d *Decoder) loadUnicode() error {
sline := string(line) sline := string(line)
d.buf.Reset() d.buf.Reset()
d.buf.Grow(len(line)) // approximation
for len(sline) > 0 { for len(sline) > 0 {
var r rune var r rune
...@@ -925,6 +928,7 @@ func (d *Decoder) loadShortBinUnicode() error { ...@@ -925,6 +928,7 @@ func (d *Decoder) loadShortBinUnicode() error {
} }
d.buf.Reset() d.buf.Reset()
d.buf.Grow(int(b))
_, err = io.CopyN(&d.buf, d.r, int64(b)) _, err = io.CopyN(&d.buf, d.r, int64(b))
if err != nil { if err != nil {
return err return err
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment