Documentation
¶
Index ¶
- Constants
- func DisableMemoryTracking()
- func EnableMemoryTracking()
- func GetBuffer(size BufferSize) []byte
- func GetStringBuilder(size PoolSize) *strings.Builder
- func IsMemoryTrackingEnabled() bool
- func PutBuffer(buffer []byte, size BufferSize)
- func PutStringBuilder(builder *strings.Builder, size PoolSize)
- func ResetBufferPoolMetrics()
- func ResetMemoryMetrics()
- func ResetPoolMetrics()
- func TrackAllocation(bytes int64)
- func TrackBufferReuse()
- func TrackOperation(duration time.Duration)
- func TrackPoolHitRate(hitRate float64)
- func TrackStringReuse()
- func WithBuffer(size BufferSize, fn func([]byte))
- func WithBufferReturn[T any](size BufferSize, fn func([]byte) T) T
- func WithStringBuilder(size PoolSize, fn func(*strings.Builder) string) string
- type BufferPool
- type BufferPoolMetrics
- type BufferSize
- type MemoryMetrics
- type MemoryTracker
- func (mt *MemoryTracker) Disable()
- func (mt *MemoryTracker) Enable()
- func (mt *MemoryTracker) GetMetrics() MemoryMetrics
- func (mt *MemoryTracker) IsEnabled() bool
- func (mt *MemoryTracker) Reset()
- func (mt *MemoryTracker) TrackAllocation(bytes int64)
- func (mt *MemoryTracker) TrackBufferReuse()
- func (mt *MemoryTracker) TrackOperation(duration time.Duration)
- func (mt *MemoryTracker) TrackPoolHitRate(hitRate float64)
- func (mt *MemoryTracker) TrackStringReuse()
- func (mt *MemoryTracker) UpdateGCTimestamp()
- type PoolMetrics
- type PoolSize
- type StringBuilderPool
- type SystemMemoryStats
Constants ¶
const ( // Buffer size categories for different use cases SmallBufferSize = 4096 // 4KB - for small I/O operations MediumBufferSize = 32768 // 32KB - for medium I/O operations LargeBufferSize = 131072 // 128KB - for large I/O operations // 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 )
const ( // Pool size thresholds for different operation types SmallStringBuilderSize = 256 // For small string operations MediumStringBuilderSize = 1024 // For medium string operations (like AsFlags) LargeStringBuilderSize = 4096 // For large string operations DefaultBufferSize = 8192 // Default buffer size for I/O operations )
Variables ¶
This section is empty.
Functions ¶
func DisableMemoryTracking ¶
func DisableMemoryTracking()
DisableMemoryTracking disables memory tracking in the default tracker
func EnableMemoryTracking ¶
func EnableMemoryTracking()
EnableMemoryTracking enables memory tracking in the default tracker
func GetBuffer ¶
func GetBuffer(size BufferSize) []byte
GetBuffer gets a buffer from the default pool
func GetStringBuilder ¶
GetStringBuilder gets a string builder from the default pool
func IsMemoryTrackingEnabled ¶
func IsMemoryTrackingEnabled() bool
IsMemoryTrackingEnabled returns whether memory tracking is enabled in the default tracker
func PutBuffer ¶
func PutBuffer(buffer []byte, size BufferSize)
PutBuffer returns a buffer to the default pool
func PutStringBuilder ¶
PutStringBuilder returns a string builder to the default pool
func ResetBufferPoolMetrics ¶
func ResetBufferPoolMetrics()
ResetBufferPoolMetrics resets all buffer pool metrics (useful for testing)
func ResetMemoryMetrics ¶
func ResetMemoryMetrics()
ResetMemoryMetrics resets all memory metrics in the default tracker
func ResetPoolMetrics ¶
func ResetPoolMetrics()
ResetPoolMetrics resets all pool metrics (useful for testing)
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 ¶
TrackOperation records an operation duration using the default tracker
func TrackPoolHitRate ¶
func TrackPoolHitRate(hitRate float64)
TrackPoolHitRate updates pool hit rate 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 result 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 byte slice from the appropriate pool
func (*BufferPool) GetMetrics ¶
func (p *BufferPool) GetMetrics() BufferPoolMetrics
GetMetrics returns buffer pool efficiency metrics
func (*BufferPool) Put ¶
func (p *BufferPool) Put(buffer []byte, size BufferSize)
Put returns a byte slice to the appropriate pool
type BufferPoolMetrics ¶
type BufferPoolMetrics struct {
SmallHits int64
SmallMisses int64
MediumHits int64
MediumMisses int64
LargeHits int64
LargeMisses int64
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 buffer pool
func (BufferPoolMetrics) HitRate ¶
func (m BufferPoolMetrics) HitRate() float64
HitRate calculates the overall hit rate across all buffer pools
type BufferSize ¶
type BufferSize int
BufferSize represents different buffer size categories
const ( SmallBuffer BufferSize = iota MediumBuffer LargeBuffer HashBuffer TarBuffer )
func EstimateBufferSize ¶
func EstimateBufferSize(estimatedDataSize int) BufferSize
EstimateBufferSize estimates the appropriate buffer size based on expected data size
type MemoryMetrics ¶
type MemoryMetrics struct {
LastGCTimestamp time.Time
TotalAllocations int64
TotalAllocBytes int64
PeakAllocBytes int64
OperationCount int64
TotalDuration time.Duration
PoolHitRate float64
BufferReuseCount int64
StringReuseCount int64
}
MemoryMetrics contains memory usage and performance statistics
func GetMemoryMetrics ¶
func GetMemoryMetrics() MemoryMetrics
GetMemoryMetrics returns current memory metrics from the default tracker
func WithMemoryTracking ¶
func WithMemoryTracking(fn func()) MemoryMetrics
WithMemoryTracking executes a function with memory tracking enabled
func (MemoryMetrics) AverageAllocationSize ¶
func (m MemoryMetrics) AverageAllocationSize() int64
AverageAllocationSize calculates the average allocation size
func (MemoryMetrics) AverageOperationDuration ¶
func (m MemoryMetrics) AverageOperationDuration() time.Duration
AverageOperationDuration calculates the average duration per operation
func (MemoryMetrics) ReuseEfficiency ¶
func (m MemoryMetrics) ReuseEfficiency() float64
ReuseEfficiency calculates the efficiency of resource reuse
type MemoryTracker ¶
type MemoryTracker struct {
// contains filtered or unexported fields
}
MemoryTracker tracks memory usage and performance metrics
func NewMemoryTracker ¶
func NewMemoryTracker() *MemoryTracker
NewMemoryTracker creates a new memory tracker instance
func (*MemoryTracker) GetMetrics ¶
func (mt *MemoryTracker) GetMetrics() MemoryMetrics
GetMetrics returns current memory metrics
func (*MemoryTracker) IsEnabled ¶
func (mt *MemoryTracker) IsEnabled() bool
IsEnabled returns whether memory tracking is enabled
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) TrackPoolHitRate ¶
func (mt *MemoryTracker) TrackPoolHitRate(hitRate float64)
TrackPoolHitRate updates the pool hit rate
func (*MemoryTracker) TrackStringReuse ¶
func (mt *MemoryTracker) TrackStringReuse()
TrackStringReuse increments string reuse counter
func (*MemoryTracker) UpdateGCTimestamp ¶
func (mt *MemoryTracker) UpdateGCTimestamp()
UpdateGCTimestamp updates the last garbage collection timestamp
type PoolMetrics ¶
type PoolMetrics struct {
SmallHits int64
SmallMisses int64
MediumHits int64
MediumMisses int64
LargeHits int64
LargeMisses 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 overall hit rate across all pools
type PoolSize ¶
type PoolSize int
PoolSize represents different pool size categories
func EstimateSize ¶
EstimateSize estimates the appropriate pool size based on expected content
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 with optimized configurations
func (*StringBuilderPool) Get ¶
func (p *StringBuilderPool) Get(size PoolSize) *strings.Builder
Get retrieves a string.Builder from the appropriate pool
func (*StringBuilderPool) GetMetrics ¶
func (p *StringBuilderPool) GetMetrics() PoolMetrics
GetMetrics returns pool efficiency metrics
type SystemMemoryStats ¶
type SystemMemoryStats struct {
LastGC time.Time
HeapAlloc uint64
Sys uint64
Lookups uint64
Mallocs uint64
Frees uint64
Alloc uint64
HeapSys uint64
HeapIdle uint64
HeapInuse uint64
HeapReleased uint64
GCCPUFraction float64
TotalAlloc uint64
NumGC uint32
}
SystemMemoryStats returns current system memory statistics
func GetSystemMemoryStats ¶
func GetSystemMemoryStats() SystemMemoryStats
GetSystemMemoryStats returns current system memory statistics