]> Cypherpunks repositories - gostls13.git/commit
bytes: rely on runtime.growslice for growing
authorJoe Tsai <joetsai@digital-static.net>
Tue, 14 Sep 2021 21:26:11 +0000 (14:26 -0700)
committerJoseph Tsai <joetsai@digital-static.net>
Thu, 10 Mar 2022 16:24:16 +0000 (16:24 +0000)
commit2d026a4ea5b28680c1ed7660f720c4cb028c5b35
tree1fee69cc0480974313fece85c759c622d4b6ce36
parent1cf67709beb2d7e317cb6e6b983f3fc6304ebc1a
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>
src/bytes/buffer.go
src/bytes/buffer_test.go