From bf51c4698b5764b350191bff3cf821235db1e073 Mon Sep 17 00:00:00 2001 From: Albert Date: Wed, 29 Jun 2016 00:46:18 +0500 Subject: [PATCH] Small refactoring (#4) * Move some methods to pool.go * Use method instead of manual resetting of buffer * Fix typo --- bytebuffer.go | 19 ------------------- pool.go | 17 ++++++++++++++++- 2 files changed, 16 insertions(+), 20 deletions(-) diff --git a/bytebuffer.go b/bytebuffer.go index 0e83cfe..7ea3ce5 100644 --- a/bytebuffer.go +++ b/bytebuffer.go @@ -57,22 +57,3 @@ func (b *ByteBuffer) SetString(s string) { func (b *ByteBuffer) Reset() { b.B = b.B[:0] } - -// Get returns an empty byte buffer from the pool. -// -// Getd byte buffer may be returned to the pool via Put call. -// This reduces the number of memory allocations required for byte buffer -// management. -func Get() *ByteBuffer { - return defaultPool.Get() -} - -// Put returns byte buffer to the pool. -// -// ByteBuffer.B mustn't be touched after returning it to the pool. -// Otherwise data races will occur. -func Put(b *ByteBuffer) { - defaultPool.Put(b) -} - -var defaultPool Pool diff --git a/pool.go b/pool.go index 0a2c8cc..fae4f95 100644 --- a/pool.go +++ b/pool.go @@ -32,6 +32,15 @@ type Pool struct { pool sync.Pool } +var defaultPool Pool + +// Get returns an empty byte buffer from the pool. +// +// Got byte buffer may be returned to the pool via Put call. +// This reduces the number of memory allocations required for byte buffer +// management. +func Get() *ByteBuffer { return defaultPool.Get() } + // Get returns new byte buffer with zero length. // // The byte buffer may be returned to the pool via Put after the use @@ -46,6 +55,12 @@ func (p *Pool) Get() *ByteBuffer { } } +// Put returns byte buffer to the pool. +// +// ByteBuffer.B mustn't be touched after returning it to the pool. +// Otherwise data races will occur. +func Put(b *ByteBuffer) { defaultPool.Put(b) } + // Put releases byte buffer obtained via Get to the pool. // // The bufer mustn't be accessed after returning to the pool. @@ -58,7 +73,7 @@ func (p *Pool) Put(b *ByteBuffer) { maxSize := int(atomic.LoadUint64(&p.maxSize)) if maxSize == 0 || cap(b.B) <= maxSize { - b.B = b.B[:0] + b.Reset() p.pool.Put(b) } }