diff --git a/common/buf/buffer_pool.go b/common/buf/buffer_pool.go index fac6aa30..59a4fb89 100644 --- a/common/buf/buffer_pool.go +++ b/common/buf/buffer_pool.go @@ -15,14 +15,19 @@ func createAllocFunc(size uint32) func() interface{} { } } +// The following parameters controls the size of buffer pools. +// There are numPools pools. Starting from 2k size, the size of each pool is sizeMulti of the previous one. +// Package buf is guaranteed to not use buffers larger than the largest pool. +// Other packets may use larger buffers. const ( numPools = 5 sizeMulti = 4 ) var ( - pool [numPools]sync.Pool - poolSize [numPools]uint32 + pool [numPools]sync.Pool + poolSize [numPools]uint32 + largeSize uint32 ) func init() { @@ -32,6 +37,7 @@ func init() { New: createAllocFunc(size), } poolSize[i] = size + largeSize = size size *= sizeMulti } } diff --git a/common/buf/reader.go b/common/buf/reader.go index 8ad3bba1..a9e20c90 100644 --- a/common/buf/reader.go +++ b/common/buf/reader.go @@ -19,8 +19,6 @@ func NewBytesToBufferReader(reader io.Reader) Reader { } } -const xlSize = 128 * 1024 - func (r *BytesToBufferReader) readSmall() (MultiBuffer, error) { b := New() for i := 0; i < 64; i++ { @@ -55,7 +53,7 @@ func (r *BytesToBufferReader) ReadMultiBuffer() (MultiBuffer, error) { if nBytes > 0 { mb := NewMultiBufferCap(nBytes/Size + 1) mb.Write(r.buffer[:nBytes]) - if nBytes == len(r.buffer) && nBytes < xlSize { + if nBytes == len(r.buffer) && nBytes < int(largeSize) { freeBytes(r.buffer) r.buffer = newBytes(uint32(nBytes) + 1) } else if nBytes < Size { diff --git a/common/buf/reader_test.go b/common/buf/reader_test.go index f5964ff9..36cd003b 100644 --- a/common/buf/reader_test.go +++ b/common/buf/reader_test.go @@ -33,7 +33,7 @@ func TestAdaptiveReader(t *testing.T) { b, err = reader.ReadMultiBuffer() assert(err, IsNil) - assert(b.Len(), Equals, 128*1024) + assert(b.Len(), Equals, 512*1024) } func TestBytesReaderWriteTo(t *testing.T) {