From 32fddadd98f938018485fba6253d30273db4e5e9 Mon Sep 17 00:00:00 2001 From: Russ Cox Date: Thu, 25 Jun 2015 19:27:20 -0400 Subject: 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 --- src/runtime/runtime_test.go | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) (limited to 'src/runtime/runtime_test.go') 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 + } + } +} -- cgit v1.3-5-g9baa