diff options
author | Filip Gruszczyński <gruszczy@gmail.com> | 2017-04-09 10:44:39 -0700 |
---|---|---|
committer | Brad Fitzpatrick <bradfitz@golang.org> | 2017-05-03 02:43:40 +0000 |
commit | f504bc00554d6ad05bd81c0a36babb735feeb37e (patch) | |
tree | 595f2f3295ddfb4bffab297a29bdcf80aadb95ef /src/encoding/gob/decode.go | |
parent | f9531448b8abf7bb3c2702761bd8792a3bef33a0 (diff) | |
download | go-f504bc00554d6ad05bd81c0a36babb735feeb37e.tar.gz go-f504bc00554d6ad05bd81c0a36babb735feeb37e.zip |
encoding/gob: use MakeMapWithSize when decoding map
This allows to pre-allocate the final size of the hashmap and avoid
re-allocating as we insert entries. Furthermore for the current
implementation of the hashmap it allows avoiding several rounds of
evacuating hashmap entries after each re-allocation.
DecodeComplex128Slice-8 51.9µs ± 1% 51.9µs ± 2% ~ (p=0.797 n=30+29)
DecodeFloat64Slice-8 31.5µs ± 2% 31.6µs ± 2% ~ (p=0.050 n=28+28)
DecodeInt32Slice-8 32.0µs ± 2% 31.9µs ± 3% ~ (p=0.666 n=29+28)
DecodeStringSlice-8 57.7µs ± 2% 57.8µs ± 3% ~ (p=0.780 n=27+30)
DecodeInterfaceSlice-8 498µs ± 2% 495µs ± 2% ~ (p=0.070 n=28+29)
DecodeMap-8 300µs ± 2% 230µs ± 5% -23.31% (p=0.000 n=27+27)
Updates #19525
Change-Id: Ia7233da49f05bae7a86c064d9ecebca966f5f2f7
Reviewed-on: https://go-review.googlesource.com/40113
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Diffstat (limited to 'src/encoding/gob/decode.go')
-rw-r--r-- | src/encoding/gob/decode.go | 5 |
1 files changed, 2 insertions, 3 deletions
diff --git a/src/encoding/gob/decode.go b/src/encoding/gob/decode.go index 013f71ccdb..879d6d2b77 100644 --- a/src/encoding/gob/decode.go +++ b/src/encoding/gob/decode.go @@ -557,11 +557,10 @@ func decodeIntoValue(state *decoderState, op decOp, isPtr bool, value reflect.Va // Because the internals of maps are not visible to us, we must // use reflection rather than pointer magic. func (dec *Decoder) decodeMap(mtyp reflect.Type, state *decoderState, value reflect.Value, keyOp, elemOp decOp, ovfl error) { + n := int(state.decodeUint()) if value.IsNil() { - // Allocate map. - value.Set(reflect.MakeMap(mtyp)) + value.Set(reflect.MakeMapWithSize(mtyp, n)) } - n := int(state.decodeUint()) keyIsPtr := mtyp.Key().Kind() == reflect.Ptr elemIsPtr := mtyp.Elem().Kind() == reflect.Ptr keyInstr := &decInstr{keyOp, 0, nil, ovfl} |