github.com/laof/lite-speed-test@v0.0.0-20230930011949-1f39b7037845/common/pool/pool.go (about) 1 package pool 2 3 import ( 4 "bytes" 5 "sync" 6 ) 7 8 const ( 9 // io.Copy default buffer size is 32 KiB 10 // but the maximum packet size of vmess/shadowsocks is about 16 KiB 11 // so define a buffer of 20 KiB to reduce the memory of each TCP relay 12 RelayBufferSize = 20 * 1024 13 ) 14 15 func Get(size int) []byte { 16 return defaultAllocator.Get(size) 17 } 18 19 func Put(buf []byte) error { 20 return defaultAllocator.Put(buf) 21 } 22 23 var bufferPool = sync.Pool{New: func() interface{} { return &bytes.Buffer{} }} 24 25 // FIXME 26 func GetBuffer() *bytes.Buffer { 27 return bufferPool.Get().(*bytes.Buffer) 28 } 29 30 func PutBuffer(buf *bytes.Buffer) { 31 buf.Reset() 32 bufferPool.Put(buf) 33 }