From deedd3bb82c7ade24baf4b6542f4f16c2074c4ec Mon Sep 17 00:00:00 2001 From: Steven Allen Date: Sun, 21 Jan 2018 10:57:30 -0800 Subject: [PATCH] support large slices They won't use the buffer pool but it doesn't hurt to be nice to the user by allocating such slices anyways. --- pool.go | 13 ++++++++----- pool_test.go | 14 +++++++------- 2 files changed, 15 insertions(+), 12 deletions(-) diff --git a/pool.go b/pool.go index 6a03299..6e7a8c8 100644 --- a/pool.go +++ b/pool.go @@ -48,15 +48,18 @@ type BufferPool struct { // values returned by Get. // // If no suitable buffer exists in the pool, Get creates one. -func (p *BufferPool) Get(length uint32) []byte { +func (p *BufferPool) Get(length int) []byte { if length == 0 { return nil } - idx := nextLogBase2(length) + if length > MaxLength { + return make([]byte, length) + } + idx := nextLogBase2(uint32(length)) if buf := p.pools[idx].Get(); buf != nil { - return buf.([]byte)[:length] + return buf.([]byte)[:uint32(length)] } - return make([]byte, 1<= %d, got %d", j, len(v)) } ch <- v @@ -115,7 +115,7 @@ func TestPoolStressByteSlicePool(t *testing.T) { func BenchmarkPool(b *testing.B) { var p BufferPool b.RunParallel(func(pb *testing.PB) { - i := uint32(7) + i := 7 for pb.Next() { if i > 1<<20 { i = 7 @@ -135,7 +135,7 @@ func BenchmarkPoolOverlflow(b *testing.B) { bufs := make([][]byte, 2100) for pow := uint32(0); pow < 21; pow++ { for i := 0; i < 100; i++ { - bufs = append(bufs, p.Get(uint32(1<