Go Runtime Performance

Measurement playbook

  1. Reproduce
  2. Profile (CPU, allocs, trace)
  3. Classify (CPU/alloc/lock/IO)
  4. Hypothesize
  5. Verify
  6. Guard with benchmarks

Tooling commands

  • go test -bench . -benchmem
  • go tool pprof -http=:0 cpu.out
  • go test -trace trace.out + go tool trace
  • GODEBUG=gctrace=1 for 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
Use arrow keys or click edges to navigate. Press H to toggle help, F for fullscreen.