From 2ade7ce230df0476794e721bfd3af28cefdaca3d Mon Sep 17 00:00:00 2001 From: Steven Allen Date: Fri, 17 Nov 2017 12:06:31 -0800 Subject: [PATCH] Preallocate pools 1. They're small. 2. We can avoid the lock (major speedup). 3. Better memory locality. 4. Much simpler. --- pool.go | 15 ++------------- 1 file changed, 2 insertions(+), 13 deletions(-) diff --git a/pool.go b/pool.go index df8fa92..80d7991 100644 --- a/pool.go +++ b/pool.go @@ -40,9 +40,7 @@ const MaxLength = 1 << 32 // Pool is a pool to handle cases of reusing elements of varying sizes. // It maintains up to 32 internal pools, for each power of 2 in 0-32. type Pool struct { - small int // the size of the first pool - pools [32]*sync.Pool // a list of singlePools - sync.Mutex // protecting list + pools [32]sync.Pool // a list of singlePools // New is a function that constructs a new element in the pool, with given len New func(len int) interface{} @@ -52,16 +50,7 @@ func (p *Pool) getPool(idx uint32) *sync.Pool { if idx > uint32(len(p.pools)) { panic(fmt.Errorf("index too large: %d", idx)) } - - p.Lock() - defer p.Unlock() - - sp := p.pools[idx] - if sp == nil { - sp = new(sync.Pool) - p.pools[idx] = sp - } - return sp + return &p.pools[idx] } // Get selects an arbitrary item from the Pool, removes it from the Pool, -- GitLab