mirror of
https://github.com/golang/go
synced 2024-11-19 21:34:45 -07:00
runtime: add maxSliceCap
This avoids expensive division calculations for many common slice element sizes. name old time/op new time/op delta MakeSlice-8 51.9ns ± 3% 35.1ns ± 2% -32.41% (p=0.000 n=10+10) GrowSliceBytes-8 44.1ns ± 2% 44.1ns ± 1% ~ (p=0.984 n=10+10) GrowSliceInts-8 60.9ns ± 3% 60.9ns ± 3% ~ (p=0.698 n=10+10) GrowSlicePtr-8 131ns ± 1% 120ns ± 2% -8.41% (p=0.000 n=8+10) GrowSliceStruct24Bytes-8 111ns ± 2% 103ns ± 3% -7.23% (p=0.000 n=8+8) Change-Id: I2630eb3d73c814db030cad16e620ea7fecbbd312 Reviewed-on: https://go-review.googlesource.com/22223 Reviewed-by: Keith Randall <khr@golang.org>
This commit is contained in:
parent
55ab07c224
commit
a4dd6ea152
@ -792,7 +792,7 @@ func newarray(typ *_type, n uintptr) unsafe.Pointer {
|
|||||||
if typ.kind&kindNoPointers != 0 {
|
if typ.kind&kindNoPointers != 0 {
|
||||||
flags |= flagNoScan
|
flags |= flagNoScan
|
||||||
}
|
}
|
||||||
if int(n) < 0 || (typ.size > 0 && n > _MaxMem/typ.size) {
|
if int(n) < 0 || n > maxSliceCap(typ.size) {
|
||||||
panic(plainError("runtime: allocation size out of range"))
|
panic(plainError("runtime: allocation size out of range"))
|
||||||
}
|
}
|
||||||
return mallocgc(typ.size*n, typ, flags)
|
return mallocgc(typ.size*n, typ, flags)
|
||||||
|
@ -14,6 +14,28 @@ type slice struct {
|
|||||||
cap int
|
cap int
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// maxElems is a lookup table containing the maximum capacity for a slice.
|
||||||
|
// The index is the size of the slice element.
|
||||||
|
var maxElems = [...]uintptr{
|
||||||
|
^uintptr(0),
|
||||||
|
_MaxMem / 1, _MaxMem / 2, _MaxMem / 3, _MaxMem / 4,
|
||||||
|
_MaxMem / 5, _MaxMem / 6, _MaxMem / 7, _MaxMem / 8,
|
||||||
|
_MaxMem / 9, _MaxMem / 10, _MaxMem / 11, _MaxMem / 12,
|
||||||
|
_MaxMem / 13, _MaxMem / 14, _MaxMem / 15, _MaxMem / 16,
|
||||||
|
_MaxMem / 17, _MaxMem / 18, _MaxMem / 19, _MaxMem / 20,
|
||||||
|
_MaxMem / 21, _MaxMem / 22, _MaxMem / 23, _MaxMem / 24,
|
||||||
|
_MaxMem / 25, _MaxMem / 26, _MaxMem / 27, _MaxMem / 28,
|
||||||
|
_MaxMem / 29, _MaxMem / 30, _MaxMem / 31, _MaxMem / 32,
|
||||||
|
}
|
||||||
|
|
||||||
|
// maxSliceCap returns the maximum capacity for a slice.
|
||||||
|
func maxSliceCap(elemsize uintptr) uintptr {
|
||||||
|
if elemsize < uintptr(len(maxElems)) {
|
||||||
|
return maxElems[elemsize]
|
||||||
|
}
|
||||||
|
return _MaxMem / elemsize
|
||||||
|
}
|
||||||
|
|
||||||
// TODO: take uintptrs instead of int64s?
|
// TODO: take uintptrs instead of int64s?
|
||||||
func makeslice(t *slicetype, len64, cap64 int64) slice {
|
func makeslice(t *slicetype, len64, cap64 int64) slice {
|
||||||
// NOTE: The len > maxElements check here is not strictly necessary,
|
// NOTE: The len > maxElements check here is not strictly necessary,
|
||||||
@ -22,11 +44,7 @@ func makeslice(t *slicetype, len64, cap64 int64) slice {
|
|||||||
// but since the cap is only being supplied implicitly, saying len is clearer.
|
// but since the cap is only being supplied implicitly, saying len is clearer.
|
||||||
// See issue 4085.
|
// See issue 4085.
|
||||||
|
|
||||||
maxElements := ^uintptr(0)
|
maxElements := maxSliceCap(t.elem.size)
|
||||||
if t.elem.size > 0 {
|
|
||||||
maxElements = _MaxMem / t.elem.size
|
|
||||||
}
|
|
||||||
|
|
||||||
len := int(len64)
|
len := int(len64)
|
||||||
if len64 < 0 || int64(len) != len64 || uintptr(len) > maxElements {
|
if len64 < 0 || int64(len) != len64 || uintptr(len) > maxElements {
|
||||||
panic(errorString("makeslice: len out of range"))
|
panic(errorString("makeslice: len out of range"))
|
||||||
@ -84,27 +102,24 @@ func growslice(t *slicetype, old slice, cap int) slice {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var lenmem, capmem, maxcap uintptr
|
var lenmem, capmem uintptr
|
||||||
const ptrSize = unsafe.Sizeof((*byte)(nil))
|
const ptrSize = unsafe.Sizeof((*byte)(nil))
|
||||||
switch et.size {
|
switch et.size {
|
||||||
case 1:
|
case 1:
|
||||||
lenmem = uintptr(old.len)
|
lenmem = uintptr(old.len)
|
||||||
capmem = roundupsize(uintptr(newcap))
|
capmem = roundupsize(uintptr(newcap))
|
||||||
newcap = int(capmem)
|
newcap = int(capmem)
|
||||||
maxcap = _MaxMem
|
|
||||||
case ptrSize:
|
case ptrSize:
|
||||||
lenmem = uintptr(old.len) * ptrSize
|
lenmem = uintptr(old.len) * ptrSize
|
||||||
capmem = roundupsize(uintptr(newcap) * ptrSize)
|
capmem = roundupsize(uintptr(newcap) * ptrSize)
|
||||||
newcap = int(capmem / ptrSize)
|
newcap = int(capmem / ptrSize)
|
||||||
maxcap = _MaxMem / ptrSize
|
|
||||||
default:
|
default:
|
||||||
lenmem = uintptr(old.len) * et.size
|
lenmem = uintptr(old.len) * et.size
|
||||||
capmem = roundupsize(uintptr(newcap) * et.size)
|
capmem = roundupsize(uintptr(newcap) * et.size)
|
||||||
newcap = int(capmem / et.size)
|
newcap = int(capmem / et.size)
|
||||||
maxcap = _MaxMem / et.size
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if cap < old.cap || uintptr(newcap) > maxcap {
|
if cap < old.cap || uintptr(newcap) > maxSliceCap(et.size) {
|
||||||
panic(errorString("growslice: cap out of range"))
|
panic(errorString("growslice: cap out of range"))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user