mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
bytes: don't compact Buffer so aggressively
benchmark old ns/op new ns/op delta BenchmarkBufferNotEmptyWriteRead 848416 819983 -3.35% Update #5154 R=golang-dev, gri, robryk CC=golang-dev https://golang.org/cl/8173043
This commit is contained in:
parent
994f59666f
commit
43e38d5def
2 changed files with 9 additions and 5 deletions
|
|
@ -87,9 +87,11 @@ func (b *Buffer) grow(n int) int {
|
||||||
var buf []byte
|
var buf []byte
|
||||||
if b.buf == nil && n <= len(b.bootstrap) {
|
if b.buf == nil && n <= len(b.bootstrap) {
|
||||||
buf = b.bootstrap[0:]
|
buf = b.bootstrap[0:]
|
||||||
} else if m+n <= cap(b.buf) {
|
} else if m+n <= cap(b.buf)/2 {
|
||||||
// We can slide things down instead of
|
// We can slide things down instead of allocating a new
|
||||||
// allocating a new slice.
|
// slice. We only need m+n <= cap(b.buf) to slide, but
|
||||||
|
// we instead let capacity get twice as large so we
|
||||||
|
// don't spend all our time copying.
|
||||||
copy(b.buf[:], b.buf[b.off:])
|
copy(b.buf[:], b.buf[b.off:])
|
||||||
buf = b.buf[:m]
|
buf = b.buf[:m]
|
||||||
} else {
|
} else {
|
||||||
|
|
|
||||||
|
|
@ -490,8 +490,10 @@ func TestBufferGrowth(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
cap1 := b.Cap()
|
cap1 := b.Cap()
|
||||||
if cap1 > cap0 {
|
// (*Buffer).grow allows for 2x capacity slop before sliding,
|
||||||
t.Errorf("buffer cap = %d; too big", cap1)
|
// so set our error threshold at 3x.
|
||||||
|
if cap1 > cap0*3 {
|
||||||
|
t.Errorf("buffer cap = %d; too big (grew from %d)", cap1, cap0)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue