Go Runtime Performance
Measurement playbook
- Reproduce
- Profile (CPU, allocs, trace)
- Classify (CPU/alloc/lock/IO)
- Hypothesize
- Verify
- Guard with benchmarks
Tooling commands
go test -bench . -benchmemgo tool pprof -http=:0 cpu.outgo test -trace trace.out+go tool traceGODEBUG=gctrace=1for GC lines
Allocations
- Track allocs/op and bytes/op
- Preallocate slices/maps
- Avoid string/byte conversions
GC
- Cost scales with live heap
- Reduce long‑lived objects
- Prefer compact data
- Tune with
GOGC/GOMEMLIMIT
Goroutines + scheduler
- Coarse‑grained goroutines
- Batch work; avoid lock contention
- M/P/G model; cgo and syscalls can pin threads
Memory model
- Synchronization establishes happens‑before
- No data races (ever)
- Use channels/atomics for safe publication
Generics
- Measure generic vs specialized
- Avoid interface boxing in hot paths
- Constrained types help inlining
Zero‑alloc patterns
- Caller‑provided buffers
- No fmt on hot paths
- Short‑buffer errors
1 / 1