Blog
Profile CPU Before Optimizing The Code You Suspect
The slow-looking method in a code review is often irrelevant. The hot path is the path that consumes measured time under real load.
Establish The Condition
Record throughput, latency, CPU utilization, allocation rate, GC activity, and the exact workload during the regression. High latency with low CPU points toward waiting, locks, I/O, or downstream limits—not arithmetic optimization.
Capture a profile long enough to represent the problem and short enough to avoid mixing unrelated traffic. Prefer sampling profilers for production investigations; measure their overhead in your environment.
Read Both Dimensions
CPU samples show where processors execute. Wall-clock or off-CPU views reveal sleeping, blocking, and lock contention. A thread waiting on a connection pool barely consumes CPU but can dominate request latency.
In a flame graph, width represents accumulated samples, not “bad code.” Follow the widest stacks, then connect them to a request or job that matters.
Change One Thing
Form a concrete hypothesis, make one change, repeat the same load, and compare profiles as well as user-visible latency. Microbenchmarks are useful only when they reproduce the relevant operation and avoid dead-code or warm-up traps.
Optimization without a baseline is storytelling with faster syntax.