pool

package
v1.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 11, 2026 License: Apache-2.0 Imports: 3 Imported by: 0

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

View Source
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

View Source
var BytesBufferPool = sync.Pool{
	New: func() any {
		return bytes.NewBuffer(make([]byte, 0, MediumBufferSize))
	},
}

BytesBufferPool provides pooled bytes.Buffer instances.

View Source
var LargeByteSlicePool = sync.Pool{
	New: func() any {
		b := make([]byte, LargeBufferSize)
		return &b
	},
}

LargeByteSlicePool provides pooled large byte slices.

View Source
var SmallByteSlicePool = sync.Pool{
	New: func() any {
		b := make([]byte, SmallBufferSize)
		return &b
	},
}

SmallByteSlicePool provides pooled small byte slices.

View Source
var StringBuilderPool = sync.Pool{
	New: func() any {
		b := &strings.Builder{}
		b.Grow(MediumBufferSize)
		return b
	},
}

StringBuilderPool provides pooled strings.Builder instances.

Functions

func GetBytesBuffer

func GetBytesBuffer() *bytes.Buffer

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

func GetStringBuilder() *strings.Builder

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

func PutBytesBuffer(buf *bytes.Buffer)

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

func PutStringBuilder(sb *strings.Builder)

PutStringBuilder returns a strings.Builder to the pool. The builder should not be used after calling this function.

func WithBytesBuffer

func WithBytesBuffer(fn func(*bytes.Buffer))

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

func WithStringBuilder(fn func(*strings.Builder))

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL