Commit 15d618fd authored by Kirill Smelkov's avatar Kirill Smelkov

decoder: Avoid allocation in readLine

readLine is used in many text decoders, so it pays off if we care not to
do allocation for result on every readLine call:

    name      old time/op    new time/op    delta
    Speed-4      378ns ± 0%     379ns ± 1%     ~     (p=0.397 n=5+5)
    Decode-4    70.1µs ± 0%    67.3µs ± 0%   -3.97%  (p=0.008 n=5+5)
    Encode-4    16.6µs ± 0%    16.6µs ± 0%     ~     (p=0.548 n=5+5)

    name      old alloc/op   new alloc/op   delta
    Speed-4       280B ± 0%      280B ± 0%     ~     (all equal)
    Decode-4    32.3kB ± 0%    31.0kB ± 0%   -3.87%  (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       428 ± 0%       326 ± 0%  -23.83%  (p=0.008 n=5+5)
    Encode-4       297 ± 0%       297 ± 0%     ~     (all equal)
parent 14aaa14f
......@@ -116,6 +116,9 @@ type Decoder struct {
// a reusable buffer that can be used by the various decoding functions
// functions using this should call buf.Reset to clear the old contents
buf bytes.Buffer
// reusable buffer for readLine
line []byte
}
// NewDecoder constructs a new Decoder which will decode the pickle stream in r.
......@@ -265,21 +268,23 @@ loop:
return d.pop()
}
// readLine reads next line from pickle stream
// returned line is valid only till next call to readLine
func (d *Decoder) readLine() ([]byte, error) {
var (
line []byte
data []byte
isPrefix = true
err error
)
d.line = d.line[:0]
for isPrefix {
data, isPrefix, err = d.r.ReadLine()
if err != nil {
return line, err
return d.line, err
}
line = append(line, data...)
d.line = append(d.line, data...)
}
return line, nil
return d.line, nil
}
// Push a marker
......@@ -646,11 +651,13 @@ func (d *Decoder) global() error {
if err != nil {
return err
}
smodule := string(module)
name, err := d.readLine()
if err != nil {
return err
}
d.stack = append(d.stack, Class{Module: string(module), Name: string(name)})
sname := string(name)
d.stack = append(d.stack, Class{Module: smodule, Name: sname})
return nil
}
......
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