diff --git a/common/pool/alloc.go b/common/pool/alloc.go index 5722b047..ee3fa1a1 100644 --- a/common/pool/alloc.go +++ b/common/pool/alloc.go @@ -12,22 +12,34 @@ var defaultAllocator = NewAllocator() // Allocator for incoming frames, optimized to prevent overwriting after zeroing type Allocator struct { - buffers []sync.Pool + buffers [17]sync.Pool } // NewAllocator initiates a []byte allocator for frames less than 65536 bytes, // the waste(memory fragmentation) of space allocation is guaranteed to be // no more than 50%. func NewAllocator() *Allocator { - alloc := new(Allocator) - alloc.buffers = make([]sync.Pool, 17) // 1B -> 64K - for k := range alloc.buffers { - i := k - alloc.buffers[k].New = func() any { - return make([]byte, 1< 64K + {New: func() any { return new([1]byte) }}, + {New: func() any { return new([1 << 1]byte) }}, + {New: func() any { return new([1 << 2]byte) }}, + {New: func() any { return new([1 << 3]byte) }}, + {New: func() any { return new([1 << 4]byte) }}, + {New: func() any { return new([1 << 5]byte) }}, + {New: func() any { return new([1 << 6]byte) }}, + {New: func() any { return new([1 << 7]byte) }}, + {New: func() any { return new([1 << 8]byte) }}, + {New: func() any { return new([1 << 9]byte) }}, + {New: func() any { return new([1 << 10]byte) }}, + {New: func() any { return new([1 << 11]byte) }}, + {New: func() any { return new([1 << 12]byte) }}, + {New: func() any { return new([1 << 13]byte) }}, + {New: func() any { return new([1 << 14]byte) }}, + {New: func() any { return new([1 << 15]byte) }}, + {New: func() any { return new([1 << 16]byte) }}, + }, } - return alloc } // Get a []byte from pool with most appropriate cap @@ -40,12 +52,50 @@ func (alloc *Allocator) Get(size int) []byte { case size > 65536: return make([]byte, size) default: - bits := msb(size) - if size == 1< 65536 { return nil } - + bits := msb(cap(buf)) if cap(buf) != 1<