diff options
| author | Joe Tsai <joetsai@digital-static.net> | 2021-09-14 14:26:11 -0700 |
|---|---|---|
| committer | Joseph Tsai <joetsai@digital-static.net> | 2022-03-10 16:24:16 +0000 |
| commit | 2d026a4ea5b28680c1ed7660f720c4cb028c5b35 (patch) | |
| tree | 1fee69cc0480974313fece85c759c622d4b6ce36 /src/bytes/buffer_test.go | |
| parent | 1cf67709beb2d7e317cb6e6b983f3fc6304ebc1a (diff) | |
| download | go-2d026a4ea5b28680c1ed7660f720c4cb028c5b35.tar.xz | |
bytes: rely on runtime.growslice for growing
Rather than naively making a slice of capacity 2*c+n,
rely on the append(..., make(...)) pattern to allocate a
slice that aligns up to the closest size class.
Performance:
name old time/op new time/op delta
BufferWriteBlock/N4096 3.03µs ± 6% 2.04µs ± 6% -32.60% (p=0.000 n=10+10)
BufferWriteBlock/N65536 47.8µs ± 6% 28.1µs ± 2% -41.32% (p=0.000 n=9+8)
BufferWriteBlock/N1048576 844µs ± 7% 510µs ± 5% -39.59% (p=0.000 n=8+9)
name old alloc/op new alloc/op delta
BufferWriteBlock/N4096 12.3kB ± 0% 7.2kB ± 0% -41.67% (p=0.000 n=10+10)
BufferWriteBlock/N65536 258kB ± 0% 130kB ± 0% -49.60% (p=0.000 n=10+10)
BufferWriteBlock/N1048576 4.19MB ± 0% 2.10MB ± 0% -49.98% (p=0.000 n=10+8)
name old allocs/op new allocs/op delta
BufferWriteBlock/N4096 3.00 ± 0% 3.00 ± 0% ~ (all equal)
BufferWriteBlock/N65536 7.00 ± 0% 7.00 ± 0% ~ (all equal)
BufferWriteBlock/N1048576 11.0 ± 0% 11.0 ± 0% ~ (all equal)
The performance is faster since the growth rate is capped at 2x,
while previously it could grow by amounts potentially much greater than 2x,
leading to significant amounts of memory waste and extra copying.
Credit goes to Martin Möhrmann for suggesting the
append(b, make([]T, n)...) pattern.
Fixes #42984
Updates #51462
Change-Id: I7b23f75dddbf53f8b8b93485bb1a1fff9649b96b
Reviewed-on: https://go-review.googlesource.com/c/go/+/349994
Trust: Joseph Tsai <joetsai@digital-static.net>
Trust: Josh Bleecher Snyder <josharian@gmail.com>
Reviewed-by: Bryan Mills <bcmills@google.com>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Reviewed-by: Josh Bleecher Snyder <josharian@gmail.com>
Diffstat (limited to 'src/bytes/buffer_test.go')
| -rw-r--r-- | src/bytes/buffer_test.go | 15 |
1 files changed, 15 insertions, 0 deletions
diff --git a/src/bytes/buffer_test.go b/src/bytes/buffer_test.go index 9c9b7440ff..c0855007c1 100644 --- a/src/bytes/buffer_test.go +++ b/src/bytes/buffer_test.go @@ -672,3 +672,18 @@ func BenchmarkBufferFullSmallReads(b *testing.B) { } } } + +func BenchmarkBufferWriteBlock(b *testing.B) { + block := make([]byte, 1024) + for _, n := range []int{1 << 12, 1 << 16, 1 << 20} { + b.Run(fmt.Sprintf("N%d", n), func(b *testing.B) { + b.ReportAllocs() + for i := 0; i < b.N; i++ { + var bb Buffer + for bb.Len() < n { + bb.Write(block) + } + } + }) + } +} |
