mem

package
v1.83.1 Latest Latest
Warning

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

Go to latest
Published: Aug 19, 2026 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Package mem provides utilities that facilitate memory reuse in byte slices that are used as buffers.

Index

Constants

This section is empty.

Variables

View Source
var (
	// BufferPoolingThreshold is the minimum size of a buffer that can be pooled.
	// This is used to determine whether to pool buffers or allocate them directly.
	BufferPoolingThreshold = 1 << 10
)

Functions

This section is empty.

Types

type BinaryTieredBufferPool

type BinaryTieredBufferPool struct {
	// contains filtered or unexported fields
}

BinaryTieredBufferPool is a buffer pool that uses multiple sub-pools with power-of-two sizes.

func NewBinaryTieredBufferPool

func NewBinaryTieredBufferPool(powerOfTwoExponents ...uint8) (*BinaryTieredBufferPool, error)

NewBinaryTieredBufferPool returns a BufferPool backed by multiple sub-pools. This structure enables O(1) lookup time for Get and Put operations.

The arguments provided are the exponents for the buffer capacities (powers of 2), not the raw byte sizes. For example, to create a pool of 16KB buffers (2^14 bytes), pass 14 as the argument.

func NewDirtyBinaryTieredBufferPool

func NewDirtyBinaryTieredBufferPool(powerOfTwoExponents ...uint8) (*BinaryTieredBufferPool, error)

NewDirtyBinaryTieredBufferPool returns a BufferPool backed by multiple sub-pools. It is similar to NewBinaryTieredBufferPool but it does not initialize the buffers before returning them.

func (*BinaryTieredBufferPool) Get

func (b *BinaryTieredBufferPool) Get(size int) *[]byte

Get returns a buffer with specified length from the pool.

func (*BinaryTieredBufferPool) Put

func (b *BinaryTieredBufferPool) Put(buf *[]byte)

Put returns a buffer to the pool.

type NopBufferPool

type NopBufferPool struct{}

NopBufferPool is a buffer pool that returns new buffers without pooling.

func (NopBufferPool) Get

func (NopBufferPool) Get(length int) *[]byte

Get returns a buffer with specified length from the pool.

func (NopBufferPool) Put

func (NopBufferPool) Put(*[]byte)

Put returns a buffer to the pool.

type SimpleBufferPool added in v1.81.0

type SimpleBufferPool struct {
	// contains filtered or unexported fields
}

SimpleBufferPool is an implementation of the mem.BufferPool interface that attempts to pool buffers with a sync.Pool. When Get is invoked, it tries to acquire a buffer from the pool but if that buffer is too small, it returns it to the pool and creates a new one.

func NewDirtySimplePool added in v1.81.0

func NewDirtySimplePool() *SimpleBufferPool

NewDirtySimplePool constructs a SimpleBufferPool. It does not initialize the buffers before returning them. Callers must ensure they don't read the buffers before writing data to them.

func (*SimpleBufferPool) Get added in v1.81.0

func (p *SimpleBufferPool) Get(size int) *[]byte

Get returns a buffer with specified length from the pool.

func (*SimpleBufferPool) Put added in v1.81.0

func (p *SimpleBufferPool) Put(buf *[]byte)

Put returns a buffer to the pool.

type TieredBufferPool

type TieredBufferPool struct {
	// contains filtered or unexported fields
}

TieredBufferPool implements the BufferPool interface with multiple tiers of buffer pools for different sizes of buffers.

func NewTieredBufferPool

func NewTieredBufferPool(poolSizes ...int) *TieredBufferPool

NewTieredBufferPool returns a BufferPool implementation that uses multiple underlying pools of the given pool sizes.

func (*TieredBufferPool) Get

func (p *TieredBufferPool) Get(size int) *[]byte

Get returns a buffer with specified length from the pool.

func (*TieredBufferPool) Put

func (p *TieredBufferPool) Put(buf *[]byte)

Put returns a buffer to the pool.

Jump to

Keyboard shortcuts

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