Go Performance
Official sources (prefer)
- Diagnostics overview: https://go.dev/doc/diagnostics
- Profiling blog: https://go.dev/blog/profiling-go-programs
- pprof: https://github.com/google/pprof
- Local catalog:
E:\Code\skills\catalog\sources.yaml→languages_perf
Rules
- Measure before change — no micro-optimizations without profile evidence.
- Collect one profile type at a time (CPU vs heap vs mutex interfere).
- Optimize the hot path that shows in flamegraph / top, not guesses.
Commands
# Benchmarks
go test -bench=. -benchmem ./...
go test -bench=BenchmarkFoo -cpuprofile=cpu.out -memprofile=mem.out .
# Interactive pprof
go tool pprof -http=:0 cpu.out
go tool pprof mem.out
# HTTP pprof (import _ "net/http/pprof" + expose carefully)
# curl -o cpu.pb.gz "http://127.0.0.1:6060/debug/pprof/profile?seconds=30"
# Escape / inlining notes (debug builds)
go build -gcflags=all="-m=2" ./...
# Execution trace
go test -trace=trace.out .
go tool trace trace.out
Profile types (runtime/pprof)
| Profile | Use for |
|---|---|
| cpu | hot functions, CPU-bound work |
| heap | allocations, leaks, GC pressure |
| goroutine | leaks, stuck stacks |
| block / mutex | contention (enable rates first) |
Checklist
- Repro load exists (bench or realistic traffic)
- CPU or heap profile captured
- Top offenders mapped to code
- Change + re-bench / re-profile
- No prod pprof left open publicly
Related
performance-optimization(general)source-driven-development+ Context7 for API docsverification-before-completion