]> Cypherpunks repositories - gostls13.git/commitdiff
encoding/gob: use reflect.Value.SetZero
authorDaniel Martí <mvdan@mvdan.cc>
Sat, 25 Feb 2023 17:04:30 +0000 (17:04 +0000)
committerDaniel Martí <mvdan@mvdan.cc>
Mon, 27 Feb 2023 15:31:13 +0000 (15:31 +0000)
Particularly helps when decoding map maps.

goos: linux
goarch: amd64
pkg: encoding/gob
cpu: AMD Ryzen 7 PRO 5850U with Radeon Graphics
│     old     │                new                 │
│   sec/op    │   sec/op     vs base               │
DecodeStringSlice-16      30.78µ ± 2%   30.68µ ± 1%        ~ (p=0.699 n=6)
DecodeStringsSlice-16     61.36µ ± 6%   60.85µ ± 4%        ~ (p=0.589 n=6)
DecodeBytesSlice-16       16.57µ ± 2%   16.51µ ± 4%        ~ (p=0.937 n=6)
DecodeInterfaceSlice-16   124.3µ ± 0%   125.4µ ± 1%        ~ (p=0.065 n=6)
DecodeMap-16              251.0µ ± 3%   216.2µ ± 2%  -13.86% (p=0.002 n=6)
geomean                   62.80µ        60.87µ        -3.07%

│     old      │                new                 │
│     B/op     │     B/op      vs base              │
DecodeStringSlice-16      37.98Ki ± 0%   37.98Ki ± 0%       ~ (p=1.000 n=6)
DecodeStringsSlice-16     63.79Ki ± 0%   63.79Ki ± 0%       ~ (p=0.675 n=6)
DecodeBytesSlice-16       22.40Ki ± 0%   22.40Ki ± 0%       ~ (p=0.364 n=6)
DecodeInterfaceSlice-16   80.27Ki ± 0%   80.27Ki ± 0%       ~ (p=0.924 n=6)
DecodeMap-16              52.67Ki ± 0%   52.67Ki ± 0%       ~ (p=0.145 n=6)
geomean                   47.01Ki        47.01Ki       +0.00%

│     old     │                 new                 │
│  allocs/op  │  allocs/op   vs base                │
DecodeStringSlice-16      1.169k ± 0%   1.169k ± 0%       ~ (p=1.000 n=6) ¹
DecodeStringsSlice-16     2.178k ± 0%   2.178k ± 0%       ~ (p=1.000 n=6) ¹
DecodeBytesSlice-16        169.0 ± 0%    169.0 ± 0%       ~ (p=1.000 n=6) ¹
DecodeInterfaceSlice-16   3.178k ± 0%   3.178k ± 0%       ~ (p=1.000 n=6) ¹
DecodeMap-16               181.0 ± 0%    181.0 ± 0%       ~ (p=1.000 n=6) ¹
geomean                    756.3         756.3       +0.00%
¹ all samples are equal

Change-Id: I5ae9268b1a86296494d8569f5158ef0e78128eb1
Reviewed-on: https://go-review.googlesource.com/c/go/+/471257
Reviewed-by: Joseph Tsai <joetsai@digital-static.net>
TryBot-Result: Gopher Robot <gobot@golang.org>
Run-TryBot: Daniel Martí <mvdan@mvdan.cc>
Reviewed-by: Ian Lance Taylor <iant@google.com>
Reviewed-by: Than McIntosh <thanm@google.com>
src/encoding/gob/decode.go

index 28ceb5a0cf12aa8e7699bb77dc698fd846b1c0b1..bffe45a72a28ab69052c57b64271cd03d01f658b 100644 (file)
@@ -601,15 +601,13 @@ func (dec *Decoder) decodeMap(mtyp reflect.Type, state *decoderState, value refl
        keyInstr := &decInstr{keyOp, 0, nil, ovfl}
        elemInstr := &decInstr{elemOp, 0, nil, ovfl}
        keyP := reflect.New(mtyp.Key())
-       keyZ := reflect.Zero(mtyp.Key())
        elemP := reflect.New(mtyp.Elem())
-       elemZ := reflect.Zero(mtyp.Elem())
        for i := 0; i < n; i++ {
                key := decodeIntoValue(state, keyOp, keyIsPtr, keyP.Elem(), keyInstr)
                elem := decodeIntoValue(state, elemOp, elemIsPtr, elemP.Elem(), elemInstr)
                value.SetMapIndex(key, elem)
-               keyP.Elem().Set(keyZ)
-               elemP.Elem().Set(elemZ)
+               keyP.Elem().SetZero()
+               elemP.Elem().SetZero()
        }
 }
 
@@ -692,7 +690,7 @@ func (dec *Decoder) decodeInterface(ityp reflect.Type, state *decoderState, valu
        // Allocate the destination interface value.
        if len(name) == 0 {
                // Copy the nil interface value to the target.
-               value.Set(reflect.Zero(value.Type()))
+               value.SetZero()
                return
        }
        if len(name) > 1024 {