mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
runtime: reduce slice growth during append to 2x
The new inlined code for append assumed that it could pass the desired new cap to growslice, not the number of new elements. But growslice still interpreted the argument as the number of new elements, making it always grow by >2x (more precisely, 2x+1 rounded up to the next malloc block size). At the time, I had intended to change the other callers to use the new cap as well, but it's too late for that. Instead, introduce growslice_n for the old callers and keep growslice for the inlined (common case) caller. Fixes #11403. Filed #11419 to merge them. Change-Id: I1338b1e5b352f3be4e43641f44b652ef7195251b Reviewed-on: https://go-review.googlesource.com/11541 Reviewed-by: Austin Clements <austin@google.com>
This commit is contained in:
parent
1284d7d403
commit
32fddadd98
5 changed files with 63 additions and 11 deletions
|
|
@ -261,3 +261,43 @@ func TestBadOpen(t *testing.T) {
|
|||
t.Errorf("close()=%d, want -1", c)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAppendGrowth(t *testing.T) {
|
||||
var x []int64
|
||||
check := func(want int) {
|
||||
if cap(x) != want {
|
||||
t.Errorf("len=%d, cap=%d, want cap=%d", len(x), cap(x), want)
|
||||
}
|
||||
}
|
||||
|
||||
check(0)
|
||||
want := 1
|
||||
for i := 1; i <= 100; i++ {
|
||||
x = append(x, 1)
|
||||
check(want)
|
||||
if i&(i-1) == 0 {
|
||||
want = 2 * i
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var One = []int64{1}
|
||||
|
||||
func TestAppendSliceGrowth(t *testing.T) {
|
||||
var x []int64
|
||||
check := func(want int) {
|
||||
if cap(x) != want {
|
||||
t.Errorf("len=%d, cap=%d, want cap=%d", len(x), cap(x), want)
|
||||
}
|
||||
}
|
||||
|
||||
check(0)
|
||||
want := 1
|
||||
for i := 1; i <= 100; i++ {
|
||||
x = append(x, One...)
|
||||
check(want)
|
||||
if i&(i-1) == 0 {
|
||||
want = 2 * i
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue