diff options
| author | Russ Cox <rsc@golang.org> | 2015-06-25 19:27:20 -0400 |
|---|---|---|
| committer | Russ Cox <rsc@golang.org> | 2015-06-26 17:49:33 +0000 |
| commit | 32fddadd98f938018485fba6253d30273db4e5e9 (patch) | |
| tree | d00bea248ea247fd5133c3502225ea0c60e77f50 /src/runtime/runtime_test.go | |
| parent | 1284d7d403875d11cff97dfb7c946a7ee11e1569 (diff) | |
| download | go-32fddadd98f938018485fba6253d30273db4e5e9.tar.xz | |
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>
Diffstat (limited to 'src/runtime/runtime_test.go')
| -rw-r--r-- | src/runtime/runtime_test.go | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/src/runtime/runtime_test.go b/src/runtime/runtime_test.go index f65562ab91..75fc9bcb84 100644 --- a/src/runtime/runtime_test.go +++ b/src/runtime/runtime_test.go @@ -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 + } + } +} |
