Skip to content

Commit 3b091bf

Browse files
committed
runtime: use multiplication with overflow check for growslice
This improves performance for slices with an element size larger than 32 bytes and removes loading a value from the maxElems array for smaller element sizes. name old time/op new time/op delta GrowSlice/Byte 41.4ns ± 2% 41.5ns ± 1% ~ (p=0.366 n=10+9) GrowSlice/Int16 51.1ns ± 2% 51.0ns ± 2% ~ (p=0.985 n=10+10) GrowSlice/Int 64.0ns ± 1% 64.2ns ± 1% ~ (p=0.180 n=10+10) GrowSlice/Ptr 90.8ns ± 1% 90.7ns ± 1% ~ (p=0.858 n=9+10) GrowSlice/Struct/24 108ns ± 0% 108ns ± 2% ~ (p=0.488 n=8+9) GrowSlice/Struct/32 118ns ± 2% 117ns ± 2% ~ (p=0.327 n=10+10) GrowSlice/Struct/40 159ns ± 1% 148ns ± 1% -6.87% (p=0.000 n=10+9) Updates #21588 Change-Id: I443b82972d379b1befa791f9ee468b3adc6bb760 Reviewed-on: https://go-review.googlesource.com/c/143798 Run-TryBot: Martin Möhrmann <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]>
1 parent e85b8db commit 3b091bf

File tree

1 file changed

+11
-10
lines changed

1 file changed

+11
-10
lines changed

src/runtime/slice.go

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package runtime
66

77
import (
8+
"runtime/internal/math"
89
"runtime/internal/sys"
910
"unsafe"
1011
)
@@ -104,10 +105,11 @@ func growslice(et *_type, old slice, cap int) slice {
104105
msanread(old.array, uintptr(old.len*int(et.size)))
105106
}
106107

108+
if cap < old.cap {
109+
panic(errorString("growslice: cap out of range"))
110+
}
111+
107112
if et.size == 0 {
108-
if cap < old.cap {
109-
panic(errorString("growslice: cap out of range"))
110-
}
111113
// append should not create a slice with nil pointer but non-zero len.
112114
// We assume that append doesn't need to preserve old.array in this case.
113115
return slice{unsafe.Pointer(&zerobase), old.len, cap}
@@ -169,15 +171,14 @@ func growslice(et *_type, old slice, cap int) slice {
169171
default:
170172
lenmem = uintptr(old.len) * et.size
171173
newlenmem = uintptr(cap) * et.size
172-
capmem = roundupsize(uintptr(newcap) * et.size)
173-
overflow = uintptr(newcap) > maxSliceCap(et.size)
174+
capmem, overflow = math.MulUintptr(et.size, uintptr(newcap))
175+
capmem = roundupsize(capmem)
174176
newcap = int(capmem / et.size)
175177
}
176178

177-
// The check of overflow (uintptr(newcap) > maxSliceCap(et.size))
178-
// in addition to capmem > _MaxMem is needed to prevent an overflow
179-
// which can be used to trigger a segfault on 32bit architectures
180-
// with this example program:
179+
// The check of overflow in addition to capmem > maxAlloc is needed
180+
// to prevent an overflow which can be used to trigger a segfault
181+
// on 32bit architectures with this example program:
181182
//
182183
// type T [1<<27 + 1]int64
183184
//
@@ -188,7 +189,7 @@ func growslice(et *_type, old slice, cap int) slice {
188189
// s = append(s, d, d, d, d)
189190
// print(len(s), "\n")
190191
// }
191-
if cap < old.cap || overflow || capmem > maxAlloc {
192+
if overflow || capmem > maxAlloc {
192193
panic(errorString("growslice: cap out of range"))
193194
}
194195

0 commit comments

Comments
 (0)