]> Cypherpunks.ru repositories - gostls13.git/commitdiff
encoding/gob: use reflect.Value.Grow
authorDaniel Martí <mvdan@mvdan.cc>
Sat, 1 Apr 2023 17:55:06 +0000 (18:55 +0100)
committerDaniel Martí <mvdan@mvdan.cc>
Tue, 4 Apr 2023 07:17:19 +0000 (07:17 +0000)
Growing by one is a simpler, and often cheaper,
operation compared to appending one (newly created) zero value.
The method was introduced in Go 1.20.

growSlice in dec_helpers.go is left alone,
as it grows using the builtin append instead of reflect.Append.

No noticeable performance difference on any of our benchmarks,
as this code only runs for slices large enough to not fit in
saferio.SliceCap, and none of our benchmarks use data that large.

goos: linux
goarch: amd64
pkg: encoding/gob
cpu: AMD Ryzen 7 PRO 5850U with Radeon Graphics
   │     old     │                new                 │
   │   sec/op    │   sec/op     vs base               │
DecodeBytesSlice-8       11.37µ ± 1%   11.46µ ± 4%       ~ (p=0.315 n=10)
DecodeInterfaceSlice-8   96.49µ ± 1%   95.75µ ± 1%       ~ (p=0.436 n=10)
geomean                  33.12µ        33.12µ       +0.01%

   │     old      │                 new                 │
   │     B/op     │     B/op      vs base               │
DecodeBytesSlice-8       22.39Ki ± 0%   22.39Ki ± 0%       ~ (p=1.000 n=10)
DecodeInterfaceSlice-8   80.25Ki ± 0%   80.25Ki ± 0%       ~ (p=0.650 n=10)
geomean                  42.39Ki        42.39Ki       +0.00%

   │     old     │                 new                  │
   │  allocs/op  │  allocs/op   vs base                 │
DecodeBytesSlice-8        169.0 ± 0%    169.0 ± 0%       ~ (p=1.000 n=10) ¹
DecodeInterfaceSlice-8   3.178k ± 0%   3.178k ± 0%       ~ (p=1.000 n=10) ¹
geomean                   732.9         732.9       +0.00%

Change-Id: I468aebf4ae6f197a1fd35f6fee809ca591c1788f
Reviewed-on: https://go-review.googlesource.com/c/go/+/481376
Reviewed-by: Ian Lance Taylor <iant@google.com>
Run-TryBot: Daniel Martí <mvdan@mvdan.cc>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Rob Pike <r@golang.org>
Reviewed-by: Michael Knyszek <mknyszek@google.com>
src/encoding/gob/decode.go

index bffe45a72a28ab69052c57b64271cd03d01f658b..76ea332e5dee51205c8e1e5d0b44deecf4f50c9a 100644 (file)
@@ -381,10 +381,10 @@ func decUint8Slice(i *decInstr, state *decoderState, value reflect.Value) {
                        if i >= ln {
                                // We didn't allocate the entire slice,
                                // due to using saferio.SliceCap.
-                               // Append a value to grow the slice.
+                               // Grow the slice for one more element.
                                // The slice is full, so this should
                                // bump up the capacity.
-                               value.Set(reflect.Append(value, reflect.Zero(value.Type().Elem())))
+                               value.Grow(1)
                        }
                        // Copy into s up to the capacity or n,
                        // whichever is less.
@@ -549,8 +549,8 @@ func (dec *Decoder) decodeArrayHelper(state *decoderState, value reflect.Value,
                }
                if i >= ln {
                        // This is a slice that we only partially allocated.
-                       // Grow it using append, up to length.
-                       value.Set(reflect.Append(value, reflect.Zero(value.Type().Elem())))
+                       // Grow it up to length.
+                       value.Grow(1)
                        cp := value.Cap()
                        if cp > length {
                                cp = length