memory

package
v0.18.1 Latest Latest
Warning

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

Go to latest
Published: Jul 29, 2025 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// Specialized buffer sizes
	HashBufferSize = 65536 // 64KB - optimized for hash operations
	TarBufferSize  = 65536 // 64KB - optimized for tar operations

	// Maximum buffer size to retain in pool (prevents memory bloat)
	MaxRetainedBufferSize = 1048576 // 1MB
)
View Source
const (
	// StringBuilderSize is the default size for string operations
	StringBuilderSize = 1024 // For string operations (like AsFlags)
	DefaultBufferSize = 8192 // Default buffer size for I/O operations
)

Variables

This section is empty.

Functions

func GetBuffer

func GetBuffer(size BufferSize) []byte

GetBuffer gets a buffer from the default pool

func GetStringBuilder

func GetStringBuilder() *strings.Builder

GetStringBuilder gets a string builder from the default pool

func PutBuffer

func PutBuffer(buffer []byte, size BufferSize)

PutBuffer returns a buffer to the default pool

func PutStringBuilder

func PutStringBuilder(builder *strings.Builder)

PutStringBuilder returns a string builder to the default pool

func TrackAllocation

func TrackAllocation(bytes int64)

TrackAllocation records an allocation using the default tracker

func TrackBufferReuse

func TrackBufferReuse()

TrackBufferReuse increments buffer reuse counter using the default tracker

func TrackOperation

func TrackOperation(duration time.Duration)

TrackOperation records an operation duration using the default tracker

func TrackStringReuse

func TrackStringReuse()

TrackStringReuse increments string reuse counter using the default tracker

func WithBuffer

func WithBuffer(size BufferSize, fn func([]byte))

WithBuffer executes a function with a pooled buffer This ensures proper cleanup even if the function panics

func WithBufferReturn

func WithBufferReturn[T any](size BufferSize, fn func([]byte) T) T

WithBufferReturn executes a function with a pooled buffer and returns a value This ensures proper cleanup even if the function panics

func WithStringBuilder

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

WithStringBuilder executes a function with a pooled string builder This ensures proper cleanup even if the function panics

Types

type BufferPool

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

BufferPool manages reusable byte slices for I/O operations

func NewBufferPool

func NewBufferPool() *BufferPool

NewBufferPool creates a new BufferPool with optimized configurations

func (*BufferPool) Get

func (p *BufferPool) Get(size BufferSize) []byte

Get retrieves a buffer from the appropriate pool

func (*BufferPool) GetMetrics

func (p *BufferPool) GetMetrics() BufferPoolMetrics

GetMetrics returns pool efficiency metrics

func (*BufferPool) Put

func (p *BufferPool) Put(buffer []byte, size BufferSize)

Put returns a buffer to the appropriate pool

type BufferPoolMetrics

type BufferPoolMetrics struct {
	HashHits   int64
	HashMisses int64
	TarHits    int64
	TarMisses  int64
}

BufferPoolMetrics contains statistics about buffer pool usage

func GetBufferPoolMetrics

func GetBufferPoolMetrics() BufferPoolMetrics

GetBufferPoolMetrics returns metrics for the default pool

func (BufferPoolMetrics) HitRate

func (m BufferPoolMetrics) HitRate() float64

HitRate calculates the overall hit rate across all pools

type BufferSize

type BufferSize int

BufferSize represents different buffer size categories

const (
	HashBuffer BufferSize = iota
	TarBuffer
	// Alias for backward compatibility
	SmallBuffer = HashBuffer
)

type MemoryTracker

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

MemoryTracker tracks basic memory usage metrics for pools

func NewMemoryTracker

func NewMemoryTracker() *MemoryTracker

NewMemoryTracker creates a new memory tracker instance

func (*MemoryTracker) TrackAllocation

func (mt *MemoryTracker) TrackAllocation(bytes int64)

TrackAllocation records an allocation event

func (*MemoryTracker) TrackBufferReuse

func (mt *MemoryTracker) TrackBufferReuse()

TrackBufferReuse increments buffer reuse counter

func (*MemoryTracker) TrackOperation

func (mt *MemoryTracker) TrackOperation(duration time.Duration)

TrackOperation records the duration of an operation

func (*MemoryTracker) TrackStringReuse

func (mt *MemoryTracker) TrackStringReuse()

TrackStringReuse increments string reuse counter

type PoolMetrics

type PoolMetrics struct {
	Hits   int64
	Misses int64
}

PoolMetrics contains statistics about pool usage

func GetPoolMetrics

func GetPoolMetrics() PoolMetrics

GetPoolMetrics returns metrics for the default pool

func (PoolMetrics) HitRate

func (m PoolMetrics) HitRate() float64

HitRate calculates the hit rate

type StringBuilderPool

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

StringBuilderPool manages reusable string.Builder instances

func NewStringBuilderPool

func NewStringBuilderPool() *StringBuilderPool

NewStringBuilderPool creates a new StringBuilderPool

func (*StringBuilderPool) Get

func (p *StringBuilderPool) Get() *strings.Builder

Get retrieves a string.Builder from the pool

func (*StringBuilderPool) GetMetrics

func (p *StringBuilderPool) GetMetrics() PoolMetrics

GetMetrics returns pool efficiency metrics

func (*StringBuilderPool) Put

func (p *StringBuilderPool) Put(builder *strings.Builder)

Put returns a string.Builder to the pool

Jump to

Keyboard shortcuts

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