From f504bc00554d6ad05bd81c0a36babb735feeb37e Mon Sep 17 00:00:00 2001 From: Filip Gruszczyński Date: Sun, 9 Apr 2017 10:44:39 -0700 Subject: encoding/gob: use MakeMapWithSize when decoding map MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 TryBot-Result: Gobot Gobot Reviewed-by: Brad Fitzpatrick --- src/encoding/gob/timing_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/encoding/gob/timing_test.go') diff --git a/src/encoding/gob/timing_test.go b/src/encoding/gob/timing_test.go index 2a503ebfc5..3478bd247e 100644 --- a/src/encoding/gob/timing_test.go +++ b/src/encoding/gob/timing_test.go @@ -289,7 +289,7 @@ func BenchmarkDecodeInterfaceSlice(b *testing.B) { } func BenchmarkDecodeMap(b *testing.B) { - count := 10000 + count := 1000 m := make(map[int]int, count) for i := 0; i < count; i++ { m[i] = i @@ -303,7 +303,7 @@ func BenchmarkDecodeMap(b *testing.B) { bbuf := benchmarkBuf{data: buf.Bytes()} b.ResetTimer() for i := 0; i < b.N; i++ { - rm := make(map[int]int, 0) + var rm map[int]int bbuf.reset() dec := NewDecoder(&bbuf) err := dec.Decode(&rm) -- cgit v1.2.3-54-g00ecf