Master an evidence-driven workflow to speed up Go programs without guesswork.
-
Benchmarking & Micro-Optimizations:
-
Write realistic
testing.B
benchmarks and compare allocations with-benchmem
. -
Use table benchmarks to evaluate different algorithms (e.g., slice vs. map lookups).
-
Reduce GC pressure by reusing buffers (
bytes.Buffer
,sync.Pool
) and avoiding unnecessary allocations.
-
-
pprof & Tracing:
-
Capture CPU and heap profiles with
net/http/pprof
or on-demand files usingpprof.StartCPUProfile
. -
Interpret flame graphs to spot hot paths, lock contention, and excessive allocations.
-
Use
runtime/trace
for end-to-end latency analysis and goroutine scheduling insights.
-
-
Contended Resources:
-
Identify lock contention (
-blockprofile
, mutex profiles). -
Replace coarse-grained locks with sharded maps, atomic operations, or message passing via channels.
-
-
I/O & Serialization:
-
Batch I/O, use buffered readers/writers, and pre-size slices/maps.
-
Measure JSON vs. protobuf trade-offs; apply
json.Encoder
/Decoder
withUseNumber
to reduce conversions.
-
-
Practical Lab:
-
Start with a slow service (JSON API + DB).
-
Baseline with benchmarks, add pprof endpoints, optimize hotspots, and demonstrate before/after metrics.
-