--- description: Performance optimization guidelines --- # Performance Optimization Guidelines ## Memory Management - Use appropriate data structures for the use case - Implement proper cleanup for long-running services - Monitor memory usage and implement limits - Use memory pools for frequently allocated objects ## I/O Optimization - Use buffered I/O for better performance - Implement connection pooling for HTTP clients - Use appropriate buffer sizes (64KB for HTTP) - Minimize system calls and context switches ## Concurrency Patterns - Use worker pools for CPU-intensive tasks - Implement proper backpressure with semaphores - Use channels for coordination between goroutines - Avoid excessive goroutine creation ## Caching Strategies - Use tiered caching (memory + disk) for optimal performance - Implement intelligent cache eviction policies - Use cache warming for predictable access patterns - Monitor cache hit ratios and adjust strategies ## Network Optimization - Use HTTP/2 when available - Enable connection keep-alives - Use appropriate timeouts for different operations - Implement request coalescing for duplicate requests ## Data Structures - Choose appropriate data structures for access patterns - Use sync.RWMutex for read-heavy operations - Consider lock-free data structures where appropriate - Minimize memory allocations in hot paths ## Algorithm Selection - Choose GC algorithms based on access patterns - Use LRU for general gaming workloads - Use LFU for gaming cafes with popular content - Use Hybrid algorithms for mixed workloads ## Monitoring and Profiling - Implement performance metrics collection - Use structured logging for performance analysis - Monitor key performance indicators - Profile the application under realistic loads ## Resource Management - Implement proper resource cleanup - Use context.Context for cancellation - Set appropriate limits on resource usage - Monitor resource consumption over time ## Scalability Considerations - Design for horizontal scaling where possible - Use sharding for large datasets - Implement proper load balancing - Consider distributed caching for large deployments ## Bottleneck Identification - Profile the application to identify bottlenecks - Focus optimization efforts on the most critical paths - Use appropriate tools for performance analysis - Test performance under realistic conditions