From fcfbbf2ff68a8997438d82cc2800c4744e908854 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Daniel=20Mart=C3=AD?= Date: Sat, 25 Feb 2023 17:04:30 +0000 Subject: [PATCH] encoding/gob: use reflect.Value.SetZero MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit 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 TryBot-Result: Gopher Robot Run-TryBot: Daniel Martí Reviewed-by: Ian Lance Taylor Reviewed-by: Than McIntosh --- src/encoding/gob/decode.go | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/encoding/gob/decode.go b/src/encoding/gob/decode.go index 28ceb5a0cf..bffe45a72a 100644 --- a/src/encoding/gob/decode.go +++ b/src/encoding/gob/decode.go @@ -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 { -- 2.50.0