go-performance
Golang performance optimization patterns. Covers allocation reduction, CPU efficiency, memory layout, and benchmarking methodology.
SKILL.mdskills/go/go-performance/SKILL.md
GitHubCore Philosophy
- Profile before optimizing — Never guess where the bottleneck is. Use
pprof. - Reduce Allocations — Go's GC is fast, but reducing allocations is often the highest ROI optimization.
- Measure, Change, Re-measure — Use
benchstatto confirm that changes are statistically significant. - Document Optimizations — Explain why a pattern is used, preferably with benchmark results.
Optimization Methodology
- Write a benchmark — Isolate the function in a
_test.gofile. - Baseline — Run
go test -bench . -benchmem -count 5 > old.txt. - Identify — Use
go tool pprofto find hot spots in CPU or heap. - Optimize — Apply one change at a time.
- Compare — Run benchmarks again and use
benchstat old.txt new.txt.
Common Performance Patterns
| Technique | Goal | Example |
|---|---|---|
| Preallocation | Avoid re-allocating backing arrays | make([]int, 0, length) |
| sync.Pool | Reuse objects to reduce GC pressure | Reuse buffers or large structs |
| struct alignment | Minimize padding (smaller memory footprint) | Place larger fields before smaller ones |
| Avoid reflection | Speed up operations | Use code generation or explicit types |
| Fast String Concatenation | Reduce allocations in loops | Use strings.Builder |
| Pointers vs Values | Minimize copying or GC tracking | Use pointers for large structs (>128 bytes) |
Standard Library Tools
go test -bench: Running benchmarks.net/http/pprof: Profiling running services.runtime.MemStats: Checking memory usage at runtime.sync.Pool: Reusing temporary objects.strings.Builder: Efficiently building strings.
Benchmarking Snippet
func BenchmarkProcess(b *testing.B) {
data := prepareData()
b.ResetTimer() // Don't count setup time
for i := 0; i < b.N; i++ {
Process(data)
}
}
Anti-patterns
- ❌ Optimizing without profiling — Wasting time on things that don't matter.
- ❌ Using
reflect.DeepEqualin hot paths — It's very slow. Use typed comparisons. - ❌ Ignoring
GOMEMLIMITin containers — Can lead to OOM kills. - ❌ Logging in hot loops — Logging allocates and can be very slow.
- ❌ Unbounded goroutine spawning — Can exhaust memory and CPU.
- ❌ Speculative preallocation —
make([]T, 0, 10000)when the common case is 5.