}
func makeslice(et *_type, len, cap int) slice {
- // 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
- // 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.
- // See issue 4085.
- maxElements := maxSliceCap(et.size)
- if len < 0 || uintptr(len) > maxElements {
- panicmakeslicelen()
- }
-
- if cap < len || uintptr(cap) > maxElements {
+ mem, overflow := math.MulUintptr(et.size, uintptr(cap))
+ if overflow || mem > maxAlloc || len < 0 || len > cap {
+ // NOTE: Produce 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, but since the cap is only being
+ // supplied implicitly, saying len is clearer.
+ // See golang.org/issue/4085.
+ mem, overflow := math.MulUintptr(et.size, uintptr(len))
+ if overflow || mem > maxAlloc || len < 0 {
+ panicmakeslicelen()
+ }
panicmakeslicecap()
}
+ p := mallocgc(mem, et, true)
- p := mallocgc(et.size*uintptr(cap), et, true)
return slice{p, len, cap}
}
const N = 20
-func BenchmarkMakeSlice(b *testing.B) {
- var x []byte
- for i := 0; i < b.N; i++ {
- x = make([]byte, 32)
- _ = x
- }
-}
-
type (
struct24 struct{ a, b, c int64 }
struct32 struct{ a, b, c, d int64 }
struct40 struct{ a, b, c, d, e int64 }
)
+func BenchmarkMakeSlice(b *testing.B) {
+ const length = 2
+ b.Run("Byte", func(b *testing.B) {
+ var x []byte
+ for i := 0; i < b.N; i++ {
+ x = make([]byte, length, 2*length)
+ _ = x
+ }
+ })
+ b.Run("Int16", func(b *testing.B) {
+ var x []int16
+ for i := 0; i < b.N; i++ {
+ x = make([]int16, length, 2*length)
+ _ = x
+ }
+ })
+ b.Run("Int", func(b *testing.B) {
+ var x []int
+ for i := 0; i < b.N; i++ {
+ x = make([]int, length, 2*length)
+ _ = x
+ }
+ })
+ b.Run("Ptr", func(b *testing.B) {
+ var x []*byte
+ for i := 0; i < b.N; i++ {
+ x = make([]*byte, length, 2*length)
+ _ = x
+ }
+ })
+ b.Run("Struct", func(b *testing.B) {
+ b.Run("24", func(b *testing.B) {
+ var x []struct24
+ for i := 0; i < b.N; i++ {
+ x = make([]struct24, length, 2*length)
+ _ = x
+ }
+ })
+ b.Run("32", func(b *testing.B) {
+ var x []struct32
+ for i := 0; i < b.N; i++ {
+ x = make([]struct32, length, 2*length)
+ _ = x
+ }
+ })
+ b.Run("40", func(b *testing.B) {
+ var x []struct40
+ for i := 0; i < b.N; i++ {
+ x = make([]struct40, length, 2*length)
+ _ = x
+ }
+ })
+
+ })
+}
+
func BenchmarkGrowSlice(b *testing.B) {
b.Run("Byte", func(b *testing.B) {
x := make([]byte, 9)