Sub topic 12
Comparing before and after with differential flamegraphs
This chapter assumes you can build a Rust release binary with debug symbols, record a profile with perf, and read a basic flamegraph. A differential flamegraph shows what changed between two profiles, so you don't have to compare two separate pictures by eye.
Why compare two graphs
After you fix a hotspot, the new flamegraph looks different, but it is hard to say by how much. Widths are fractions of each run's own total samples, so a frame can look the same size in both graphs even though the program got faster overall. A diff puts both runs into one picture and colors each frame by how much it changed.
The workflow
You need two folded stack files, one from each build. A folded file has one line per unique stack, followed by the number of samples. Here is the slow is_prime from the earlier chapters:
fn is_prime(n: u64) -> bool {
if n < 2 {
return false;
}
(2..n).all(|d| n % d != 0)
}Record and fold the "before" profile:
RUSTFLAGS="-C force-frame-pointers=yes" cargo build --release
perf record -F 99 -g -- ./target/release/myapp
perf script | inferno-collapse-perf > before.foldedApply the square-root fix:
fn is_prime(n: u64) -> bool {
if n < 2 {
return false;
}
(2..).take_while(|d| d * d <= n).all(|d| n % d != 0)
}Then rebuild, record again, and fold the "after" profile. Pass both files to the diff tool:
perf script | inferno-collapse-perf > after.folded
inferno-diff-folded before.folded after.folded | inferno-flamegraph > diff.svgReading the colors
In the resulting SVG, the frame layout comes from the second profile. Colors encode the change:
| Color | Meaning |
|---|---|
| Red | The frame has more samples in the second profile |
| Blue | The frame has fewer samples in the second profile |
| Gray or white | Little or no change |
Deeper color means a larger change. After the square-root fix, you should see the is_prime frame and its callees in blue. This confirms the fix reduced time in the place you expected.
Common errors
- Different workloads. If the two runs process different input, the diff shows the input change, not your code change. Use identical input and the same run length.
- Different sample counts. A longer run collects more samples, so every frame looks "redder." By default
inferno-diff-foldeddoes not normalize totals. Add--normalizewhen the runs differ in length, but remember that normalizing hides a change in total time. - Missing frames. A frame that exists only in the first profile has no box in the new layout. The diff graph is built from the second file, so removed code is not drawn. Swap the arguments to see what disappeared.
- Noise. At 99 Hz a short run has few samples, so small color changes can be random. Repeat the recording, or run longer, before trusting small differences.
- Changed build settings. Using different
RUSTFLAGSor optimization levels changes inlining and frame names, which produces red and blue frames that have nothing to do with your fix.
Exercise
You record before.folded over a 10-second run and after.folded over a 30-second run of the same program with the same input. The diff shows nearly every frame in red. What is the likely cause, and what can you do?
Show answer
The longer run has about three times as many samples, so every frame has more samples in the second profile. Re-record with the same run length, or pass --normalize to inferno-diff-folded so the totals are scaled to match. Normalizing hides changes in total time, so check total time with a benchmark.
Exercise
You swap the arguments to inferno-diff-folded. How does the output change?
Show answer
The layout is then drawn from the other profile, so frames that exist only in the old profile become visible. The meaning of red and blue also reverses: red now marks frames that have more samples in the old profile.