Initial Commit
This commit is contained in:
32
internal/buffer/buffer.go
Normal file
32
internal/buffer/buffer.go
Normal file
@@ -0,0 +1,32 @@
|
||||
package buffer
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"sync"
|
||||
)
|
||||
|
||||
const (
|
||||
maxBufSize = 16384
|
||||
initBufSize = 1024
|
||||
)
|
||||
|
||||
var bufPool = sync.Pool{
|
||||
New: func() any {
|
||||
return bytes.NewBuffer(make([]byte, 0, initBufSize))
|
||||
},
|
||||
}
|
||||
|
||||
// Alloc gets an existing buffer or allocates a new 1kB buffer.
|
||||
func Alloc() *bytes.Buffer {
|
||||
return bufPool.Get().(*bytes.Buffer)
|
||||
}
|
||||
|
||||
// Free releases the buffer back into the pool. If the buffer
|
||||
// is over 16kB, it isn't put back into the pool to avoid excessive
|
||||
// memory usage.
|
||||
func Free(buf *bytes.Buffer) {
|
||||
if buf.Cap() <= maxBufSize {
|
||||
buf.Reset()
|
||||
bufPool.Put(buf)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user