diff options
| author | korzhao <korzhao95@gmail.com> | 2021-09-03 15:55:16 +0800 |
|---|---|---|
| committer | Ian Lance Taylor <iant@golang.org> | 2021-09-07 21:39:06 +0000 |
| commit | dcf3545774a8b2bc639f8da14dec2526b8ca9caf (patch) | |
| tree | 40ee01ab5ecab8c225a8cfb33ab77675e08ca4ec /src/encoding | |
| parent | 80783558b06741beaf41dbd198013fe3a13c9ad2 (diff) | |
| download | go-dcf3545774a8b2bc639f8da14dec2526b8ca9caf.tar.xz | |
encoding/gob: optimize decoding of slice
In CL 345572, we used the reflect.Value.SetLen method to avoid
extra memory allocation for reflect.Value.Slice.
This also applies to function decodeSlice
name old time/op new time/op delta
DecodeStringsSlice-12 96.5µs ±12% 63.0µs ± 8% -34.68% (p=0.000 n=9+10)
name old alloc/op new alloc/op delta
DecodeStringsSlice-12 89.3kB ± 0% 65.3kB ± 0% -26.89% (p=0.000 n=10+10)
name old allocs/op new allocs/op delta
DecodeStringsSlice-12 3.18k ± 0% 2.18k ± 0% -31.47% (p=0.000 n=10+10)
Change-Id: Ifdb43716cc90a265962dec022704a5571f447fd8
Reviewed-on: https://go-review.googlesource.com/c/go/+/347533
Reviewed-by: Joe Tsai <joetsai@digital-static.net>
Reviewed-by: Rob Pike <r@golang.org>
Trust: Joe Tsai <joetsai@digital-static.net>
Run-TryBot: Joe Tsai <joetsai@digital-static.net>
TryBot-Result: Go Bot <gobot@golang.org>
Diffstat (limited to 'src/encoding')
| -rw-r--r-- | src/encoding/gob/decode.go | 2 | ||||
| -rw-r--r-- | src/encoding/gob/timing_test.go | 8 |
2 files changed, 8 insertions, 2 deletions
diff --git a/src/encoding/gob/decode.go b/src/encoding/gob/decode.go index 4ef9ef7243..f92556f8ab 100644 --- a/src/encoding/gob/decode.go +++ b/src/encoding/gob/decode.go @@ -625,7 +625,7 @@ func (dec *Decoder) decodeSlice(state *decoderState, value reflect.Value, elemOp if value.Cap() < n { value.Set(reflect.MakeSlice(typ, n, n)) } else { - value.Set(value.Slice(0, n)) + value.SetLen(n) } dec.decodeArrayHelper(state, value, elemOp, n, ovfl, helper) } diff --git a/src/encoding/gob/timing_test.go b/src/encoding/gob/timing_test.go index ceb21c4107..516aeea92c 100644 --- a/src/encoding/gob/timing_test.go +++ b/src/encoding/gob/timing_test.go @@ -279,7 +279,13 @@ func BenchmarkDecodeStringSlice(b *testing.B) { } benchmarkDecodeSlice(b, a) } - +func BenchmarkDecodeStringsSlice(b *testing.B) { + a := make([][]string, 1000) + for i := range a { + a[i] = []string{"now is the time"} + } + benchmarkDecodeSlice(b, a) +} func BenchmarkDecodeBytesSlice(b *testing.B) { a := make([][]byte, 1000) for i := range a { |
