aboutsummaryrefslogtreecommitdiff
path: root/src/encoding/gob/decode.go
diff options
context:
space:
mode:
authorFilip Gruszczyński <gruszczy@gmail.com>2017-03-30 19:09:56 -0700
committerIan Lance Taylor <iant@golang.org>2017-05-26 21:06:24 +0000
commit11ab865d6f42bcbfbe67219bd591518a1e1e8faf (patch)
tree1079d9df47606da6fa780c2d548758acc150ce9f /src/encoding/gob/decode.go
parent2cb3d1d8936b2aa009edbde7696dde48961c77b3 (diff)
downloadgo-11ab865d6f42bcbfbe67219bd591518a1e1e8faf.tar.gz
go-11ab865d6f42bcbfbe67219bd591518a1e1e8faf.zip
encoding/gob: speedup decoding of maps by zeroing values
Instead of allocating a new reflect.Value object on every loop we zero it. DecodeComplex128Slice-8 13.1µs ± 7% 13.2µs ± 8% ~ (p=0.347 n=18+19) DecodeFloat64Slice-8 8.13µs ± 5% 8.00µs ± 3% ~ (p=0.168 n=20+19) DecodeInt32Slice-8 8.27µs ± 5% 8.08µs ± 5% -2.27% (p=0.001 n=19+18) DecodeStringSlice-8 17.9µs ±12% 17.8µs ±11% ~ (p=0.989 n=20+19) DecodeInterfaceSlice-8 163µs ±10% 159µs ± 4% ~ (p=0.057 n=19+19) DecodeMap-8 220µs ± 2% 183µs ± 1% -17.07% (p=0.000 n=19+18) Updates #19525 Change-Id: I27f8edd4761787f6b9928d34cefa08a34a6e25b2 Reviewed-on: https://go-review.googlesource.com/39203 Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org> Reviewed-by: Rob Pike <r@golang.org> Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org>
Diffstat (limited to 'src/encoding/gob/decode.go')
-rw-r--r--src/encoding/gob/decode.go10
1 files changed, 8 insertions, 2 deletions
diff --git a/src/encoding/gob/decode.go b/src/encoding/gob/decode.go
index 879d6d2b77..8dece42e90 100644
--- a/src/encoding/gob/decode.go
+++ b/src/encoding/gob/decode.go
@@ -565,10 +565,16 @@ func (dec *Decoder) decodeMap(mtyp reflect.Type, state *decoderState, value refl
elemIsPtr := mtyp.Elem().Kind() == reflect.Ptr
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, allocValue(mtyp.Key()), keyInstr)
- elem := decodeIntoValue(state, elemOp, elemIsPtr, allocValue(mtyp.Elem()), elemInstr)
+ 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)
}
}