Commit cad218c8 authored by Kirill Smelkov's avatar Kirill Smelkov

decode: Use Decoder by pointer always

Most of the functions in ogorek.go already use Decoder by pointer, but
NewDecoder() and Decoder.Decode() exceptionally used it by value.

This was probably an oversight because when Decoder struct is used by
value it is copied on every Decode call and also it is not possible to
change and retain state in between calls as all changed happen to copy
and then forgotten.

This also leads to many unnnecessary allocations, i.e. memory allocated
for stack one Decode call is not reused on next Decode call etc.

So use Decoder by pointer only. This leads to the following speedup:

    name      old time/op    new time/op    delta
    Speed-4      377ns ± 0%     383ns ± 4%     ~     (p=0.214 n=5+5)
    Decode-4    80.4µs ± 3%    72.1µs ± 1%  -10.38%  (p=0.008 n=5+5)
    Encode-4    16.5µs ± 1%    16.5µs ± 0%     ~     (p=0.690 n=5+5)

    name      old alloc/op   new alloc/op   delta
    Speed-4       280B ± 0%      280B ± 0%     ~     (all equal)
    Decode-4    44.0kB ± 0%    35.7kB ± 0%  -18.77%  (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       502 ± 0%       429 ± 0%  -14.54%  (p=0.008 n=5+5)
    Encode-4       297 ± 0%       297 ± 0%     ~     (all equal)
parent 3979c069
......@@ -119,13 +119,13 @@ type Decoder struct {
}
// NewDecoder constructs a new Decoder which will decode the pickle stream in r.
func NewDecoder(r io.Reader) Decoder {
func NewDecoder(r io.Reader) *Decoder {
reader := bufio.NewReader(r)
return Decoder{r: reader, stack: make([]interface{}, 0), memo: make(map[string]interface{})}
return &Decoder{r: reader, stack: make([]interface{}, 0), memo: make(map[string]interface{})}
}
// Decode decodes the pickle stream and returns the result or an error.
func (d Decoder) Decode() (interface{}, error) {
func (d *Decoder) Decode() (interface{}, error) {
insn := 0
loop:
......
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