This package provides performance optimization utilities for the nginx-ui log processing system.
This package consolidates performance optimization code that was previously duplicated across indexer, parser, and searcher packages. The utilities focus on reducing memory allocations, improving concurrency, and providing efficient data structures.
Byte buffer pooling for temporary string operations
pool := utils.NewStringPool()
buf := pool.Get() // Get a reusable byte buffer
str := pool.Intern("text") // Intern strings to reduce duplicates
pool.Put(buf) // Return buffer to pool
Prevents memory fragmentation and reduces GC pressure
pool := utils.NewMemoryPool()
buf := pool.Get(1024) // Get buffer with at least 1024 bytes capacity
pool.Put(buf) // Return buffer to appropriate pool
Graceful shutdown support
pool := utils.NewWorkerPool(10, 100) // 10 workers, 100 queue size
pool.Submit(func() { /* work */ }) // Submit work
pool.Close() // Shutdown gracefully
Automatic batch reset after retrieval
bp := utils.NewBatchProcessor(100)
bp.Add(item) // Add items to batch
batch := bp.GetBatch() // Get and reset batch
Detailed memory statistics
mo := utils.NewMemoryOptimizer(512 * 1024 * 1024) // 512MB threshold
mo.CheckMemoryUsage() // Trigger GC if needed
stats := mo.GetMemoryStats() // Get memory statistics
Cache hit/miss ratio tracking
pm := utils.NewPerformanceMetrics()
pm.RecordOperation(itemCount, duration, success)
pm.RecordCacheHit()
metrics := pm.GetMetrics() // Get performance snapshot
Zero-allocation string/byte conversions for performance-critical code:
BytesToStringUnsafe([]byte) stringStringToBytesUnsafe(string) []byteAppendInt([]byte, int) []byte⚠️ Warning: These functions use unsafe operations and should be used carefully.
The package includes comprehensive tests covering:
Run tests with:
go test ./internal/nginx_log/utils/... -v
Run benchmarks with:
go test ./internal/nginx_log/utils/... -bench=.
This package replaces the previous performance_optimizations.go files in:
internal/nginx_log/indexer/performance_optimizations.go (removed)internal/nginx_log/parser/performance_optimizations.go (removed)internal/nginx_log/searcher/performance_optimizations.go (removed)The consolidated implementation provides:
StringPool for frequent string operations and temporary buffersMemoryPool for variable-size buffer allocationsWorkerPool for CPU-bound tasks requiring concurrency controlBatchProcessor for collecting items before bulk operationsMemoryOptimizer in long-running processes to manage memoryPerformanceMetrics to track and monitor system performance