From 5a2ff6af888ff76a2895a0fe453c1613996edd8b Mon Sep 17 00:00:00 2001 From: AlexStocks Date: Tue, 19 Oct 2021 22:12:25 +0800 Subject: [PATCH] release large buffer --- bytes/buffer.go | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/bytes/buffer.go b/bytes/buffer.go index 623f88e..6d876df 100644 --- a/bytes/buffer.go +++ b/bytes/buffer.go @@ -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 {