aboutsummaryrefslogtreecommitdiff
path: root/src/runtime/slice.go
diff options
context:
space:
mode:
authorRuss Cox <rsc@golang.org>2015-06-25 19:27:20 -0400
committerRuss Cox <rsc@golang.org>2015-06-26 17:49:33 +0000
commit32fddadd98f938018485fba6253d30273db4e5e9 (patch)
treed00bea248ea247fd5133c3502225ea0c60e77f50 /src/runtime/slice.go
parent1284d7d403875d11cff97dfb7c946a7ee11e1569 (diff)
downloadgo-32fddadd98f938018485fba6253d30273db4e5e9.tar.gz
go-32fddadd98f938018485fba6253d30273db4e5e9.zip
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/slice.go')
-rw-r--r--src/runtime/slice.go14
1 files changed, 12 insertions, 2 deletions
diff --git a/src/runtime/slice.go b/src/runtime/slice.go
index 15820a5181..5cda11d9b0 100644
--- a/src/runtime/slice.go
+++ b/src/runtime/slice.go
@@ -33,12 +33,22 @@ func makeslice(t *slicetype, len64, cap64 int64) slice {
return slice{p, len, cap}
}
-func growslice(t *slicetype, old slice, n int) slice {
+// growslice_n is a variant of growslice that takes the number of new elements
+// instead of the new minimum capacity.
+// TODO(rsc): This is used by append(slice, slice...).
+// The compiler should change that code to use growslice directly (issue #11419).
+func growslice_n(t *slicetype, old slice, n int) slice {
if n < 1 {
panic(errorString("growslice: invalid n"))
}
+ return growslice(t, old, old.cap+n)
+}
- cap := old.cap + n
+// growslice handles slice growth during append.
+// It is passed the slice type, the old slice, and the desired new minimum capacity,
+// and it returns a new slice with at least that capacity, with the old data
+// copied into it.
+func growslice(t *slicetype, old slice, cap int) slice {
if cap < old.cap || t.elem.size > 0 && uintptr(cap) > _MaxMem/uintptr(t.elem.size) {
panic(errorString("growslice: cap out of range"))
}