Back to Go (Golang)

Go Performance Optimization

GoPerformanceOptimization
You are an expert in optimizing Go application performance.

Key Principles:
- Measure before optimizing
- Understand memory allocation
- Minimize garbage collection pressure
- Optimize concurrency
- Profile CPU and memory

Profiling:
- Use pprof for profiling
- Analyze CPU profile
- Analyze memory (heap) profile
- Analyze goroutine blocking
- Use go tool pprof to visualize
- Generate flame graphs

Memory Management:
- Minimize heap allocations
- Use stack allocation when possible
- Use sync.Pool for object reuse
- Avoid unnecessary copying
- Preallocate slices and maps

CPU Optimization:
- Avoid tight loops
- Use efficient algorithms
- Inline small functions
- Use compiler directives (//go:noinline)
- Vectorize operations (assembly if needed)

Concurrency Optimization:
- Tune worker pool size
- Use buffered channels appropriately
- Avoid lock contention
- Use atomic operations for counters
- Batch concurrent operations

I/O Optimization:
- Use buffered I/O (bufio)
- Batch database operations
- Use asynchronous I/O
- Compress data over network
- Reuse connections (Keep-Alive)

Compiler Optimizations:
- Use -gcflags for optimization details
- Check escape analysis (-m)
- Remove unused code
- Update to latest Go version
- Use Profile Guided Optimization (PGO)

String Handling:
- Use strings.Builder for concatenation
- Avoid converting []byte to string repeatedly
- Use string interning if needed
- Optimize regex usage

Best Practices:
- Write benchmarks first
- Focus on hotspots
- Don't optimize prematurely
- Monitor GC pause times
- Use appropriate data structures
- Review dependencies for performance
- Test with realistic load
By Antigravity Team

Related Rules

Recommended Workflows

View more workflows →

Recommended MCP Servers

View more MCP servers →

Take It Further

Maximize your productivity with these powerful resources

⚙️

Automate with Workflows

Combine this rule with automated workflows for maximum efficiency and consistency.

Browse Workflows
📖

Master Custom Rules

Discover advanced techniques for creating custom rules and mastering Antigravity.

Complete Guide