Sub topic 8
Inlining, generics and mangled names
This chapter assumes you can build a Rust release binary, run cargo flamegraph, and read the basic shape of a flamegraph. Here you learn why some frames in a graph are missing, duplicated, or hard to read, and what to do about each case.
Why the graph does not match your source
A flamegraph shows what the compiler produced, not what you wrote. In release mode the optimizer rewrites your code in three ways that change the graph:
- Inlining copies a function's body into its caller, so the function has no stack frame of its own.
- Monomorphization generates a separate copy of a generic function for each concrete type it is used with.
- Name mangling encodes the path, generics and a hash into each symbol name so the linker can tell them apart.
Inlining removes frames
Take this program:
#[inline(always)]
fn square(x: u64) -> u64 {
x * x
}
fn sum_squares(limit: u64) -> u64 {
(0..limit).map(square).sum()
}
fn main() {
println!("{}", sum_squares(500_000_000));
}With inline(always), the compiler pastes square into sum_squares. A profiler that reads only the stack pointers sees no call to square. The time shows up under sum_squares or even main, because sum_squares may itself be inlined into main.
With debug = true in [profile.release], the debug info records which source lines came from which inlined function. Tools such as perf and cargo flamegraph can use that data to rebuild the inlined frames, so square often reappears in the graph. This works well but is not guaranteed. If a function you expect is missing, assume it was inlined before you assume it never ran.
Generics produce one copy per type
Rust compiles generic code once for each concrete type. A function like this becomes two separate symbols:
fn total<T: Copy + std::iter::Sum<T>>(items: &[T]) -> T {
items.iter().copied().sum()
}
fn main() {
let a: Vec<u32> = (0..1_000).collect();
let b: Vec<f64> = (0..1_000).map(|n| n as f64).collect();
println!("{} {}", total(&a), total(&b));
}The flamegraph can show total<u32> and total<f64> as two different boxes. Each box is narrower than the combined cost. When you judge a hotspot, add up the copies. Calls to Vec<T> or Iterator methods are the most common case, because every type you use creates another copy.
Reading mangled names
The linker sees symbol names like _ZN5myapp8is_prime17h3f9a2c1d4e5b6a78E. This is the legacy mangling scheme. It has the crate name, the function name and a hash ending in E. Most tools demangle these names for you, so you usually see myapp::is_prime.
If your graph shows raw _ZN... or _R... names, the demangler was skipped. You can demangle one name by hand with rustfilt:
cargo install rustfilt
echo "_ZN5myapp8is_prime17h3f9a2c1d4e5b6a78E" | rustfiltThe output is myapp::is_prime. Demangled names can still be long. Closures appear as {{closure}}, and trait methods appear as <Type as Trait>::method. Use the search box in the SVG (Ctrl+F) to find a name by a short fragment such as is_prime.
Common errors
| Symptom | Likely cause | What to do |
|---|---|---|
| A function you wrote is missing | It was inlined | Check that `debug = true` is set, or use `#[inline(never)]` temporarily |
| The same function appears in several boxes | Generic copies or different callers | Sum the widths before judging |
| Names look like `_ZN...` | Demangling was skipped | Pipe through `rustfilt` |
| Frames named `[unknown]` | No symbols or broken stacks | Rebuild with debug info and check unwinding |
Exercise
A flamegraph of your release build shows main as very wide but no frame for your small helper parse_digit, which you know is called in a hot loop. Give two explanations and one way to test each.
Show answer
First, the compiler inlined parse_digit into its caller, so its time is counted in the caller's frame. Test this by adding #[inline(never)] to parse_digit and profiling again. The function should appear with its own box. Second, the build lacks debug info, so the profiler cannot recover inlined frames or names. Test this by confirming debug = true under [profile.release] in Cargo.toml, rebuilding, and profiling again.