Documentation
¶
Overview ¶
Package pool provides memory pooling utilities for efficient buffer reuse. It reduces garbage collection pressure in hot paths by reusing allocated buffers.
Index ¶
- Constants
- Variables
- func GetBytesBuffer() *bytes.Buffer
- func GetLargeByteSlice() []byte
- func GetSmallByteSlice() []byte
- func GetStringBuilder() *strings.Builder
- func PutBytesBuffer(buf *bytes.Buffer)
- func PutLargeByteSlice(b []byte)
- func PutSmallByteSlice(b []byte)
- func PutStringBuilder(sb *strings.Builder)
- func WithBytesBuffer(fn func(*bytes.Buffer))
- func WithStringBuilder(fn func(*strings.Builder))
Constants ¶
const ( // SmallBufferSize is used for small strings and error messages. SmallBufferSize = 256 // MediumBufferSize is used for typical text processing. MediumBufferSize = 4096 // LargeBufferSize is used for report generation and file content. LargeBufferSize = 65536 )
Buffer pool sizes for different use cases.
Variables ¶
var BytesBufferPool = sync.Pool{ New: func() any { return bytes.NewBuffer(make([]byte, 0, MediumBufferSize)) }, }
BytesBufferPool provides pooled bytes.Buffer instances.
var LargeByteSlicePool = sync.Pool{ New: func() any { b := make([]byte, LargeBufferSize) return &b }, }
LargeByteSlicePool provides pooled large byte slices.
var SmallByteSlicePool = sync.Pool{ New: func() any { b := make([]byte, SmallBufferSize) return &b }, }
SmallByteSlicePool provides pooled small byte slices.
var StringBuilderPool = sync.Pool{ New: func() any { b := &strings.Builder{} b.Grow(MediumBufferSize) return b }, }
StringBuilderPool provides pooled strings.Builder instances.
Functions ¶
func GetBytesBuffer ¶
GetBytesBuffer returns a bytes.Buffer from the pool. The buffer is reset before being returned. Call PutBytesBuffer when done to return it to the pool.
func GetLargeByteSlice ¶
func GetLargeByteSlice() []byte
GetLargeByteSlice returns a large byte slice from the pool. Call PutLargeByteSlice when done to return it to the pool.
func GetSmallByteSlice ¶
func GetSmallByteSlice() []byte
GetSmallByteSlice returns a small byte slice from the pool. Call PutSmallByteSlice when done to return it to the pool.
func GetStringBuilder ¶
GetStringBuilder returns a strings.Builder from the pool. The builder is reset before being returned. Call PutStringBuilder when done to return it to the pool.
func PutBytesBuffer ¶
PutBytesBuffer returns a bytes.Buffer to the pool. The buffer should not be used after calling this function.
func PutLargeByteSlice ¶
func PutLargeByteSlice(b []byte)
PutLargeByteSlice returns a large byte slice to the pool. The slice should not be used after calling this function.
func PutSmallByteSlice ¶
func PutSmallByteSlice(b []byte)
PutSmallByteSlice returns a small byte slice to the pool. The slice should not be used after calling this function.
func PutStringBuilder ¶
PutStringBuilder returns a strings.Builder to the pool. The builder should not be used after calling this function.
func WithBytesBuffer ¶
WithBytesBuffer executes a function with a pooled bytes.Buffer. The buffer is automatically returned to the pool after the function completes. This is the preferred way to use pooled buffers for simple use cases.
func WithStringBuilder ¶
WithStringBuilder executes a function with a pooled strings.Builder. The builder is automatically returned to the pool after the function completes. This is the preferred way to use pooled builders for simple use cases.
Types ¶
This section is empty.