1
0
mirror of https://github.com/golang/go synced 2024-11-23 06:50:05 -07:00

encoding/gob: use reflect.Value.Grow

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>
This commit is contained in:
Daniel Martí 2023-04-01 18:55:06 +01:00
parent 56e900d9f0
commit f62c9701b4

View 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