Skip to content

Commit

Permalink
release large buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexStocks committed Oct 19, 2021
1 parent 7828a36 commit 5a2ff6a
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions bytes/buffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,11 +149,13 @@ func (b *Buffer) grow(n int) int {
}
c := cap(b.buf)
if n <= c/2-m {
// We can slide things down instead of allocating a new
// slice. We only need m+n <= c 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:])
// decrease buffer space
bufLen := len(b.buf[b.off:]) + n
if bufLen < smallBufferSize {
bufLen = smallBufferSize
}
newBuf := make([]byte, 0, bufLen)
b.buf = append(newBuf, b.buf[b.off:]...)
} else if c > maxInt-c-n {
panic(ErrTooLarge)
} else {
Expand Down

0 comments on commit 5a2ff6a

Please sign in to comment.