File tree 2 files changed +9
-5
lines changed 2 files changed +9
-5
lines changed Original file line number Diff line number Diff line change @@ -87,9 +87,11 @@ func (b *Buffer) grow(n int) int {
87
87
var buf []byte
88
88
if b .buf == nil && n <= len (b .bootstrap ) {
89
89
buf = b .bootstrap [0 :]
90
- } else if m + n <= cap (b .buf ) {
91
- // We can slide things down instead of
92
- // allocating a new slice.
90
+ } else if m + n <= cap (b .buf )/ 2 {
91
+ // We can slide things down instead of allocating a new
92
+ // slice. We only need m+n <= cap(b.buf) to slide, but
93
+ // we instead let capacity get twice as large so we
94
+ // don't spend all our time copying.
93
95
copy (b .buf [:], b .buf [b .off :])
94
96
buf = b .buf [:m ]
95
97
} else {
Original file line number Diff line number Diff line change @@ -490,8 +490,10 @@ func TestBufferGrowth(t *testing.T) {
490
490
}
491
491
}
492
492
cap1 := b .Cap ()
493
- if cap1 > cap0 {
494
- t .Errorf ("buffer cap = %d; too big" , cap1 )
493
+ // (*Buffer).grow allows for 2x capacity slop before sliding,
494
+ // so set our error threshold at 3x.
495
+ if cap1 > cap0 * 3 {
496
+ t .Errorf ("buffer cap = %d; too big (grew from %d)" , cap1 , cap0 )
495
497
}
496
498
}
497
499
You can’t perform that action at this time.
0 commit comments