From ee98c9419fd1dc67b66210cc373032a634085426 Mon Sep 17 00:00:00 2001 From: Aliaksandr Valialkin Date: Thu, 23 Jun 2016 17:43:05 +0300 Subject: [PATCH] Keep zero-length buffers in the pool --- pool.go | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/pool.go b/pool.go index 9f3f5df..26d5c28 100644 --- a/pool.go +++ b/pool.go @@ -33,20 +33,24 @@ func (p *byteBufferPool) Acquire() *ByteBuffer { } func (p *byteBufferPool) Release(b *ByteBuffer) { - n := cap(b.B) - if n > maxSize { + bCap := cap(b.B) + if bCap > maxSize { // Oversized buffer. // Drop it. return } - if (n >> 2) > len(b.B) { + bLen := len(b.B) + if bLen > 0 && (bCap>>2) > bLen { // Under-used buffer capacity. // Drop it. + // + // Special case: do not drop zero-length buffers - + // this may be the result of Reset call. return } b.B = b.B[:0] - idx := bitSize(n-1) >> minBitSize + idx := bitSize(bCap-1) >> minBitSize p.pools[idx].Put(b) }