Benchmarking hot paths (allocs and throughput)

9323
0

When latency matters, I benchmark the smallest interesting unit and watch allocations. In Go, a surprising number of regressions come from accidental heap churn: converting []byte to string, building lots of temporary maps, or using fmt.Sprintf in loops. A benchmark with b.ReportAllocs() and b.ResetTimer() gives you fast feedback. The other key is realism: seed representative payload sizes so the benchmark reflects production. Once a baseline exists, it’s easy to see whether a refactor made things worse. I also like to run benchmarks with -benchmem and keep an eye on allocs/op; it’s often a better leading indicator than raw ns/op. Benchmarks aren’t for micro-optimizing everything—just the paths you call millions of times.