func BenchmarkGrowSliceBytes(b *testing.B) {
b.StopTimer()
- var x = make([]byte, 8)
+ var x = make([]byte, 9)
b.StartTimer()
for i := 0; i < b.N; i++ {
_ = append([]byte(nil), x...)
func BenchmarkGrowSliceInts(b *testing.B) {
b.StopTimer()
- var x = make([]int, 8)
+ var x = make([]int, 9)
b.StartTimer()
for i := 0; i < b.N; i++ {
_ = append([]int(nil), x...)
}
}
+func BenchmarkGrowSlicePtr(b *testing.B) {
+ b.StopTimer()
+ var x = make([]*byte, 9)
+ b.StartTimer()
+ for i := 0; i < b.N; i++ {
+ _ = append([]*byte(nil), x...)
+ }
+}
+
+type struct24 struct{ a, b, c int64 }
+
+func BenchmarkGrowSliceStruct24Bytes(b *testing.B) {
+ b.StopTimer()
+ var x = make([]struct24, 9)
+ b.StartTimer()
+ for i := 0; i < b.N; i++ {
+ _ = append([]struct24(nil), x...)
+ }
+}
+
func BenchmarkAppend(b *testing.B) {
b.StopTimer()
x := make([]int, 0, N)
return slice{unsafe.Pointer(&zerobase), old.len, cap}
}
- maxcap := _MaxMem / et.size
- if cap < old.cap || uintptr(cap) > maxcap {
- panic(errorString("growslice: cap out of range"))
- }
-
newcap := old.cap
doublecap := newcap + newcap
if cap > doublecap {
newcap += newcap / 4
}
}
- if uintptr(newcap) > maxcap {
- panic(errorString("growslice: cap out of range"))
- }
}
- lenmem := uintptr(old.len) * et.size
- capmem := roundupsize(uintptr(newcap) * et.size)
- if et.size == 1 {
+ var lenmem, capmem, maxcap uintptr
+ const ptrSize = unsafe.Sizeof((*byte)(nil))
+ switch et.size {
+ case 1:
+ lenmem = uintptr(old.len)
+ capmem = roundupsize(uintptr(newcap))
newcap = int(capmem)
- } else {
+ maxcap = _MaxMem
+ case ptrSize:
+ lenmem = uintptr(old.len) * ptrSize
+ capmem = roundupsize(uintptr(newcap) * ptrSize)
+ newcap = int(capmem / ptrSize)
+ maxcap = _MaxMem / ptrSize
+ default:
+ lenmem = uintptr(old.len) * et.size
+ capmem = roundupsize(uintptr(newcap) * et.size)
newcap = int(capmem / et.size)
+ maxcap = _MaxMem / et.size
+ }
+
+ if cap < old.cap || uintptr(newcap) > maxcap {
+ panic(errorString("growslice: cap out of range"))
}
var p unsafe.Pointer