From 0b9607d1d648ae77d2db86a991db4a1fe921dbd8 Mon Sep 17 00:00:00 2001 From: Filip Gruszczyński Date: Wed, 15 Mar 2017 20:11:30 -0700 Subject: encoding/gob: Speedup map decoding by reducing the allocations. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The improvementis achieved in encoding/gob/decode.go decodeMap by allocate keyInstr and elemInstr only once and pass it to decodeIntoValue, instead of allocating a new instance on every loop cycle. name old time/op new time/op delta DecodeComplex128Slice-8 64.2µs ±10% 62.2µs ± 8% ~ (p=0.686 n=4+4) DecodeFloat64Slice-8 37.1µs ± 3% 36.5µs ± 5% ~ (p=0.343 n=4+4) DecodeInt32Slice-8 33.7µs ± 3% 32.7µs ± 4% ~ (p=0.200 n=4+4) DecodeStringSlice-8 59.7µs ± 5% 57.3µs ± 1% ~ (p=0.114 n=4+4) DecodeInterfaceSlice-8 543µs ± 7% 497µs ± 3% ~ (p=0.057 n=4+4) DecodeMap-8 3.78ms ± 8% 2.66ms ± 2% -29.69% (p=0.029 n=4+4) Updates #19525 Change-Id: Iec5fa4530de76f0a70da5de8a129a567b4aa096e Reviewed-on: https://go-review.googlesource.com/38317 Reviewed-by: Brad Fitzpatrick Reviewed-by: Rob Pike Run-TryBot: Brad Fitzpatrick TryBot-Result: Gobot Gobot --- src/encoding/gob/timing_test.go | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) (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 a7e7e683cc..e3578992fc 100644 --- a/src/encoding/gob/timing_test.go +++ b/src/encoding/gob/timing_test.go @@ -364,3 +364,28 @@ func BenchmarkDecodeInterfaceSlice(b *testing.B) { } } } + +func BenchmarkDecodeMap(b *testing.B) { + count := 10000 + m := make(map[int]int, count) + for i := 0; i < count; i++ { + m[i] = i + } + var buf bytes.Buffer + enc := NewEncoder(&buf) + err := enc.Encode(m) + if err != nil { + b.Fatal(err) + } + bbuf := benchmarkBuf{data: buf.Bytes()} + b.ResetTimer() + for i := 0; i < b.N; i++ { + rm := make(map[int]int, 0) + bbuf.reset() + dec := NewDecoder(&bbuf) + err := dec.Decode(&rm) + if err != nil { + b.Fatal(i, err) + } + } +} -- cgit v1.2.3-54-g00ecf