Sub topic 4
Building Rust for profiling: release mode with debug symbols
This chapter assumes you can build and run a Cargo project, know the difference between cargo build and cargo build --release, and understand what a call stack is. By the end you will know why a profile needs a release build, why that build needs debug information, and how to configure Cargo so profiles show function names and source lines.
Why you profile the release build
A debug build (cargo build) uses opt-level = 0. The compiler does almost no optimization. Iterators are not inlined, bounds checks and overflow checks stay in, and small helper functions remain separate calls. A profile of that binary shows where unoptimized code is slow. That is often not where optimized code is slow.
A release build (cargo build --release) uses opt-level = 3 by default. Abstractions such as iterator chains collapse into tight loops. The costs that remain are the ones that matter in production.
| Setting | Debug profile | Release profile |
|---|---|---|
| `opt-level` | 0 | 3 |
| `debug` | true (full) | false (none) |
| `debug-assertions` | true | false |
| `overflow-checks` | true | false |
| `strip` | none | none (but see the warning below) |
A debug build can be 10 to 100 times slower than a release build. The ratio differs between functions, so the ranking of hotspots can change as well. Profile what you ship.
What debug symbols are
A compiled binary contains machine code. A profiler sampling that code sees raw instruction addresses such as 0x55d1c2a4. To turn an address into a name like myapp::is_prime or a location like src/main.rs:4, the profiler needs two kinds of data:
- Symbols: a table mapping address ranges to function names. Binaries normally keep this in the symbol table.
- Debug information: DWARF data on Linux (PDB on Windows) mapping addresses to source files and lines. It also records which functions were inlined into which.
Debug information lives in the binary or in a separate file. It is not loaded into memory when the program runs, so it does not slow execution. It makes the file on disk larger.
The point that surprises many people is that debug information does not change the generated machine code. debug = true and opt-level = 3 are independent settings. You can have fully optimized code with full debug info, and that combination is what you want for profiling.
Enabling debug info in the release profile
Add this to Cargo.toml:
[profile.release]
debug = trueNow cargo build --release still optimizes at level 3 but also emits debug info. You do not need to change your source code.
The debug key accepts several values:
| Value | Meaning |
|---|---|
| `false` or `0` or `"none"` | No debug info |
| `"line-tables-only"` | File and line tables only, no variable or type info |
| `1` or `"limited"` | Line tables plus function and type names, no local variables |
| `true` or `2` or `"full"` | Everything |
For flamegraphs, you only need function names, files, and line numbers. "line-tables-only" is often enough and produces a smaller binary and faster builds. Use true when you also want to inspect variables in a debugger from the same binary.
[profile.release]
debug = "line-tables-only"Keeping your normal release profile clean
Changing [profile.release] affects every release build, including the ones you ship. Binary size grows, though speed does not change. If that bothers you, define a custom profile that inherits from release:
[profile.profiling]
inherits = "release"
debug = trueBuild and run it with the --profile flag:
cargo build --profile profilingThe output goes to target/profiling/, not target/release/. That is a common source of confusion: a command that hard-codes ./target/release/myapp will profile a stale or missing binary. Tools such as cargo flamegraph accept --profile profiling and find the right path for you.
Settings that undermine a profile
Several release options trade profile quality for size or speed. Check your Cargo.toml for them before you record anything.
[profile.release]
debug = true
strip = false
lto = false
codegen-units = 16
panic = "unwind"strip = true(or"symbols","debuginfo") removes symbols or debug info after linking. A stripped binary produces a flamegraph full of hexadecimal addresses. If you strip in your shipping profile, make sure the profiling profile setsstrip = false.lto = "fat"or"thin"enables link-time optimization. It is safe to profile with, and you should if you ship with it, because LTO changes inlining across crates. The cost is a longer build. The profile will show fewer, larger functions because more code is inlined.codegen-units = 1gives the optimizer a wider view and usually produces faster code. Again, match your shipping configuration.panic = "abort"removes unwinding tables from the binary. Stack unwinding for profiling can depend on those tables (see the later chapter on frame pointers and DWARF). If your stacks come out truncated, this is one thing to check.
The rule is to keep every setting that affects performance identical to production, and change only what affects diagnostics: debug and strip.
Dependencies and the standard library
Debug info set in [profile.release] applies to your workspace and to all dependencies compiled with that profile. That means frames from crates like serde or regex show readable names. This is usually what you want.
The standard library is different. It ships precompiled and carries limited debug info, so frames inside core and alloc generally appear with names but without full source detail. That is enough to recognize core::iter, alloc::vec, or hashbrown frames. Rebuilding the standard library with -Zbuild-std on nightly gives complete detail, but it is rarely necessary.
If a dependency is very large and you only care about your own code, you can limit debug info per package:
[profile.release]
debug = false
[profile.release.package.myapp]
debug = trueThis keeps debug info for the myapp package only. Be aware that frames from other crates then show names from the symbol table, but without line numbers.
Frame pointers
Debug info answers the question "what is this address?". A separate question is "how did execution get here?", which is how the profiler walks the call stack. Two methods exist: following frame pointers, or interpreting DWARF unwind tables. Rust release builds omit frame pointers on most targets by default, because freeing a register can make code slightly faster. Many Linux setups work with perf --call-graph dwarf, which needs no frame pointers but produces larger recordings. Later chapters cover the trade-offs. For now, you can force frame pointers with:
RUSTFLAGS="-C force-frame-pointers=yes" cargo build --releaseChanging RUSTFLAGS makes Cargo rebuild everything, including dependencies, and it caches the result separately from builds with different flags. Use the same flags each time to avoid repeated full rebuilds.
Verifying that the build is ready
Before recording, check that the binary has the information you expect. On Linux:
cargo build --release
file target/release/myapp
readelf --sections target/release/myapp | grep debugThe file output should say not stripped and, when debug info is present, with debug_info. The readelf command should list sections such as .debug_info, .debug_line, and .debug_str. If no .debug_* sections appear, Cargo did not apply your profile. Common causes are editing the wrong Cargo.toml in a workspace (profiles are only read from the workspace root) or building with a different profile than you intended.
A worked check
Use the slow prime counter that later chapters profile:
fn is_prime(n: u64) -> bool {
if n < 2 {
return false;
}
(2..n).all(|d| n % d != 0)
}
fn count_primes(limit: u64) -> usize {
(0..limit).filter(|&n| is_prime(n)).count()
}
fn main() {
println!("{}", count_primes(100_000));
}With the default release profile, a profiler would still report time in the program, but it might attribute it to addresses or to main alone. With debug = true it can name is_prime and point to the iterator line. You will see the difference directly in the next chapters.
Here is the flow from source to a readable profile:
Common mistakes
- Profiling
cargo buildoutput. The numbers are dominated by unoptimized code. - Adding
debug = trueand expecting a slowdown. It does not change speed, only file size. - Leaving
strip = truein the profile you record with. - Running
./target/release/myappafter building with a custom profile that writes totarget/profiling/. - Putting profile settings in a workspace member's
Cargo.toml, where Cargo ignores them. - Changing
RUSTFLAGSbetween runs and paying for full rebuilds each time.
Exercise
Your Cargo.toml has [profile.release] with opt-level = 3 and strip = true. A flamegraph of the release binary shows only hexadecimal addresses. List the changes you would make so that profiles show function names and line numbers, without altering the optimized code.
Show answer
Set debug = true (or "line-tables-only") and strip = false in the profile used for recording. Keep opt-level = 3 and the other performance settings (lto, codegen-units) unchanged. To avoid changing the shipping binary, put these in a custom profile such as [profile.profiling] with inherits = "release", then build with cargo build --profile profiling and profile target/profiling/myapp.
Exercise
A teammate says that enabling debug = true in the release profile makes the program slower, so profiles are unreliable. Is this correct? Explain.
Show answer
No. Debug info is stored in separate sections of the file that are not loaded during normal execution, and it does not change the generated machine code, which is set by opt-level and related options. It only increases the size of the file on disk. The profile therefore reflects the performance of the optimized program.