mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
runtime: speed up makeslice by avoiding divisions
Only compute the number of maximum allowed elements per slice once. name old time/op new time/op delta MakeSlice-2 55.5ns ± 1% 45.6ns ± 2% -17.88% (p=0.000 n=99+100) Change-Id: I951feffda5d11910a75e55d7e978d306d14da2c5 Reviewed-on: https://go-review.googlesource.com/21801 Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org>
This commit is contained in:
parent
5b3e5766bc
commit
ad7448fe98
2 changed files with 19 additions and 3 deletions
|
|
@ -7,6 +7,14 @@ import "testing"
|
||||||
|
|
||||||
const N = 20
|
const N = 20
|
||||||
|
|
||||||
|
func BenchmarkMakeSlice(b *testing.B) {
|
||||||
|
var x []byte
|
||||||
|
for i := 0; i < b.N; i++ {
|
||||||
|
x = make([]byte, 32)
|
||||||
|
_ = x
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func BenchmarkGrowSliceBytes(b *testing.B) {
|
func BenchmarkGrowSliceBytes(b *testing.B) {
|
||||||
b.StopTimer()
|
b.StopTimer()
|
||||||
var x = make([]byte, 9)
|
var x = make([]byte, 9)
|
||||||
|
|
|
||||||
|
|
@ -16,19 +16,27 @@ type slice struct {
|
||||||
|
|
||||||
// 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 > MaxMem/elemsize check here is not strictly necessary,
|
// NOTE: The len > maxElements check here is not strictly necessary,
|
||||||
// but it produces a 'len out of range' error instead of a 'cap out of range' error
|
// but it produces a 'len out of range' error instead of a 'cap out of range' error
|
||||||
// when someone does make([]T, bignumber). 'cap out of range' is true too,
|
// when someone does make([]T, bignumber). 'cap out of range' is true too,
|
||||||
// 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)
|
||||||
|
if t.elem.size > 0 {
|
||||||
|
maxElements = _MaxMem / t.elem.size
|
||||||
|
}
|
||||||
|
|
||||||
len := int(len64)
|
len := int(len64)
|
||||||
if len64 < 0 || int64(len) != len64 || t.elem.size > 0 && uintptr(len) > _MaxMem/t.elem.size {
|
if len64 < 0 || int64(len) != len64 || uintptr(len) > maxElements {
|
||||||
panic(errorString("makeslice: len out of range"))
|
panic(errorString("makeslice: len out of range"))
|
||||||
}
|
}
|
||||||
|
|
||||||
cap := int(cap64)
|
cap := int(cap64)
|
||||||
if cap < len || int64(cap) != cap64 || t.elem.size > 0 && uintptr(cap) > _MaxMem/t.elem.size {
|
if cap < len || int64(cap) != cap64 || uintptr(cap) > maxElements {
|
||||||
panic(errorString("makeslice: cap out of range"))
|
panic(errorString("makeslice: cap out of range"))
|
||||||
}
|
}
|
||||||
|
|
||||||
p := newarray(t.elem, uintptr(cap))
|
p := newarray(t.elem, uintptr(cap))
|
||||||
return slice{p, len, cap}
|
return slice{p, len, cap}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue