Sub topic 4
Building Rust for profiling: release mode with debug symbols
This chapter assumes you know basic Cargo usage, how to build in release mode, and what a call stack is. By the end you will have a build configuration that gives profilers readable function names and source lines without changing how fast your code runs.
Why the default builds fall short
A profiler samples your program and records addresses. To turn an address into a function name or a line of source, it needs symbol and debug information in the binary.
Neither build profile gives you both things you need:
cargo build(the dev profile) keeps debug info but turns optimizations off. The code is often 10 to 100 times slower than release code. Hotspots in an unoptimized build rarely match hotspots in the build you ship.cargo build --releaseturns optimizations on but, by default, includes little debug info. Flamegraphs from it show some names but no source lines, and many frames may be missing or merged.
You want the release build's optimized machine code plus debug info on top.
Turning on debug info in release
Add this to Cargo.toml:
[profile.release]
debug = trueSetting debug = true is the same as debug = 2. It makes rustc write full DWARF debug info, which maps machine code back to functions, files and line numbers. The generated machine code is unchanged. Only the binary's size and build time grow.
If the full level is too large for your project, debug = "line-tables-only" keeps file and line mappings for stack traces and is often enough for a flamegraph.
[profile.release]
debug = "line-tables-only"The optimizer decides what the code does, and the debug setting only adds metadata about it. That is why profiling a release build with debug info still reflects real performance.
Keep symbols in the binary
Symbols are the names of functions stored in the binary. Some projects strip them to shrink the executable. If your release profile has either of these, profilers lose the names:
[profile.release]
strip = trueRemove strip, or set it to "none", in the profile you use for profiling. Recent Cargo versions strip debug info by default only when you ask for it, but check any shared or inherited settings.
A separate profiling profile
Changing [profile.release] affects every release build. If you would rather not do that, define a custom profile that inherits from release:
[profile.profiling]
inherits = "release"
debug = true
strip = "none"Build and find it like this:
cargo build --profile profiling
ls target/profiling/Cargo places the output under target/profiling/, not target/release/. Tools such as cargo flamegraph accept --profile profiling to use it.
What to expect from optimized code
Optimization changes how stacks look, even with debug info present:
- Inlining merges small functions into their callers. The inlined function may vanish from the graph, or appear only through debug info.
- Tail calls and reordered code can make a stack look different from your source.
- Frame pointers, which profilers often use to walk the stack, are usually omitted. A later chapter covers this.
These effects are normal. They are the reason you profile optimized code, because they show what the CPU actually runs.
Checking your build
Confirm that debug info is present before you profile. On Linux, file reports it:
cargo build --release
file target/release/myappLook for with debug_info, not stripped in the output. If you see stripped, a strip setting or a flag from your environment is removing the information.
Exercise
Why is debug = true in [profile.release] safe to use for profiling, and which other setting in the same profile can undo its benefit?
Show answer
Debug info is metadata that maps machine code to source. It does not change the optimized code, so performance stays representative. A strip setting (such as strip = true) removes symbols and debug info from the binary and undoes the benefit.