Skip to content

Commit 1e50dd0

Browse files
committed
runtime: use multiplication with overflow check for newarray
This improves performance for e.g. maps with a bucket size (key+value*8 bytes) larger than 32 bytes and removes loading a value from the maxElems array for smaller bucket sizes. name old time/op new time/op delta MakeMap/[Byte]Byte 95.5ns ± 1% 94.7ns ± 1% -0.78% (p=0.013 n=9+9) MakeMap/[Int]Int 128ns ± 0% 121ns ± 2% -5.63% (p=0.000 n=6+10) Updates #21588 Change-Id: I7d9eb7d49150c399c15dcab675e24bc97ff97852 Reviewed-on: https://go-review.googlesource.com/c/143997 Reviewed-by: Keith Randall <[email protected]>
1 parent 286c7ae commit 1e50dd0

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

src/runtime/malloc.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ package runtime
106106

107107
import (
108108
"runtime/internal/atomic"
109+
"runtime/internal/math"
109110
"runtime/internal/sys"
110111
"unsafe"
111112
)
@@ -1040,10 +1041,11 @@ func newarray(typ *_type, n int) unsafe.Pointer {
10401041
if n == 1 {
10411042
return mallocgc(typ.size, typ, true)
10421043
}
1043-
if n < 0 || uintptr(n) > maxSliceCap(typ.size) {
1044+
mem, overflow := math.MulUintptr(typ.size, uintptr(n))
1045+
if overflow || mem > maxAlloc || n < 0 {
10441046
panic(plainError("runtime: allocation size out of range"))
10451047
}
1046-
return mallocgc(typ.size*uintptr(n), typ, true)
1048+
return mallocgc(mem, typ, true)
10471049
}
10481050

10491051
//go:linkname reflect_unsafe_NewArray reflect.unsafe_NewArray

0 commit comments

Comments
 (0)