Sub topic 4
Tuning the dev profile
This chapter covers the settings in [profile.dev] that change how much work rustc, LLVM and the linker do on every debug build. It assumes you can read a Cargo.toml, you know the difference between cargo build and cargo build --release, and you have already measured a build with cargo build --timings (see the chapter on measuring).
What a profile is
A profile is a named set of compiler settings. Cargo ships four built-in ones: dev, release, test and bench. cargo build and cargo check use dev. cargo build --release uses release. cargo test uses test, which inherits from dev.
You change a profile in the Cargo.toml at the workspace root. Profile sections in member crates are ignored, and Cargo prints a warning when it sees them.
The defaults for dev favor fast compilation of your own code and full debuggability:
| Setting | Default in `dev` | Effect |
|---|---|---|
| `opt-level` | `0` | No optimization. LLVM does the least work. |
| `debug` | `true` (full) | Emits complete DWARF debug information. |
| `incremental` | `true` | Reuses work from the previous build. |
| `codegen-units` | `256` | Splits each crate into many pieces for parallel code generation. |
| `lto` | `false` | No link-time optimization. |
| `debug-assertions` | `true` | Keeps `debug_assert!` and overflow checks. |
Most of the speed-up available here comes from debug, plus a few per-package overrides.
Reduce debug info
Full debug info lets a debugger show local variables, types and inlined frames. Producing it costs time in rustc and LLVM, and it makes object files larger. The linker then has to read and write all of that data. On large projects, debug info can account for a large share of both build time and the size of target/.
The debug setting accepts these values:
| Value | Meaning |
|---|---|
| `0` or `"none"` | No debug info. |
| `"line-tables-only"` | File and line numbers only. |
| `1` or `"limited"` | Line tables plus some type and function information. |
| `2`, `true` or `"full"` | Everything. This is the default in `dev`. |
For most projects, "line-tables-only" is a good trade. Panics and backtraces still show file names and line numbers. What you lose is the ability to inspect local variables and types in a debugger.
# Cargo.toml (workspace root)
[profile.dev]
# Keep line numbers for backtraces but skip full variable debug info.
debug = "line-tables-only"
[profile.dev.package."*"]
# Optional: optimize third-party dependencies while your own code stays fast to build.
opt-level = 2If you debug with gdb or lldb often, keep full debug info for your own crates and drop it for dependencies. Package overrides make this possible, and the next sections show how.
[profile.dev]
debug = "full"
[profile.dev.package."*"]
debug = falseThe "*" pattern matches every package in the dependency graph except workspace members. That is why it is a good target for settings that should apply to third-party code only.
Split debuginfo
On macOS and Linux you can also control where debug information lives. The split-debuginfo setting tells rustc whether to keep debug data inside the object files or to put it somewhere else.
- On macOS, the default for
devis"unpacked". The linker does not need to copy the debug data into the final binary, which saves time. - On Linux, the default is
"off". Settingsplit-debuginfo = "unpacked"can shorten link times for large binaries, although some tools handle it less well than the default.
[profile.dev]
split-debuginfo = "unpacked"Test this on your own project. The benefit depends on the linker and the size of the binary, and some debuggers and profilers need extra configuration to find the split files.
Optimize dependencies, not your own code
Debug builds of dependencies can be very slow at runtime. Code that parses JSON, decodes images or hashes data may run 10 to 100 times slower at opt-level = 0. Raising the optimization level of dependencies fixes this without touching your own crate.
Dependencies change rarely. Cargo compiles each one once and reuses the result until the dependency version, features or profile change. A higher opt-level for them therefore costs a one-time delay after a clean build or a dependency update. Your own code, which you edit constantly, stays at opt-level = 0 and rebuilds quickly.
[profile.dev.package."*"]
opt-level = 2You can also target one package by name. This is useful when a single dependency dominates runtime in your tests:
[profile.dev.package.image]
opt-level = 3
[profile.dev.package.regex]
opt-level = 2Specific package names take priority over the "*" pattern.
When this backfires
The trade-off is not free.
- A clean build gets longer, because dependencies are now compiled with the optimizer on. Check the time with
cargo build --timingsbefore and after. - If your workspace contains a crate that is large and rarely changes,
"*"does not cover it, because workspace members are excluded from the pattern. Name it explicitly if you want it optimized. - Procedural macros and build scripts are compiled for the host machine and run during the build. Optimizing them makes the code they run faster, which can shorten builds that use heavy macros. Cargo has a dedicated setting for this:
[profile.dev.build-override].
[profile.dev.build-override]
opt-level = 2Build overrides default to opt-level = 0 and debug = false. Raise opt-level only if timings show that a build script or a proc macro takes a long time to run.
Other dev settings worth knowing
`debug-assertions` and `overflow-checks`
Both are on in dev. They add runtime checks and a little compile work, but they catch bugs. Turning them off to save a few percent of build time is rarely worth it.
`incremental`
Incremental compilation is on by default in dev. Keep it on. The next chapter explains how it works and what invalidates it.
`codegen-units`
The dev default of 256 is already tuned for speed. Leave it alone. This setting matters for release builds, which a later chapter covers.
`panic = "abort"`
Aborting on panic removes unwinding code, which reduces the amount of code to generate and link. It also changes behavior. #[should_panic] tests and catch_unwind do not work with it, and the standard test harness requires unwinding. Many teams use it only in release.
A worked example
Suppose you have a workspace with three crates and a dependency on serde, tokio and image. A clean cargo build takes 74 seconds, and the debug binary is 420 MB. You apply these settings:
[workspace]
resolver = "2"
members = ["app", "core", "storage"]
[profile.dev]
debug = "line-tables-only"
[profile.dev.package."*"]
opt-level = 2Run the comparison like this:
cargo clean
cargo build --timingsOpen target/cargo-timings/cargo-timing.html and compare it with the report from before the change. Typical results from a change like this are a smaller target/ directory and a shorter link step. The clean build may get a bit longer because of opt-level = 2 on dependencies. The numbers above are an illustration. Your results depend on the project, so record your own before and after.
For the edit-compile loop, measure an incremental rebuild as well. Touch a file in your leaf crate and rebuild:
touch app/src/main.rs
time cargo buildThis is the number you feel every day, so it deserves the most weight in your decision.
How the settings interact
The diagram below shows which stage each setting influences.
Use this as a guide when choosing what to change. If --timings shows long link steps, look at debug and split-debuginfo first, then at a faster linker. If it shows a slow LLVM phase in your own crates, debug is the first lever and opt-level the second.
Common mistakes
- Putting profiles in a member crate. Cargo ignores them. Define profiles only in the workspace root.
- Setting
opt-levelfor"*"and expecting it to cover your workspace crates. It does not. Name each workspace member you want to change. - Changing settings without a baseline. A setting that helps one project can slow another. Always compare
cargo build --timingsreports. - Forgetting that
testinherits fromdev. Your changes also affectcargo test, which is usually what you want, but adebugchange can alter how test failures look in a debugger. - Mixing profile changes and other changes. Change one thing at a time so you know which one made the difference.
Summary of recommendations
- Start with
debug = "line-tables-only"unless you rely on a debugger every day. - Add
[profile.dev.package."*"] opt-level = 2if debug-mode runtime of dependencies bothers you in tests or local runs. - Try
split-debuginfo = "unpacked"on Linux and compare link times. - Leave
incremental,codegen-unitsanddebug-assertionsat their defaults. - Measure a clean build and an incremental rebuild before and after each change.
Exercise
Your debug build is fast, but a test that decodes large images takes 40 seconds to run. Which profile setting would you change, which section would you put it in, and why does it not slow down your everyday rebuilds?
Show answer
Add [profile.dev.package."*"] with opt-level = 2 (or [profile.dev.package.image] with opt-level = 3 to target one crate) in the workspace root Cargo.toml. The decoding runs inside a dependency, which the optimizer then speeds up. Dependencies are compiled once and cached until their version, features or profile change, so only your own crates, which stay at opt-level = 0, are rebuilt as you edit. You pay once for the slower first build.
Exercise
A teammate adds [profile.dev] debug = false to app/Cargo.toml, a workspace member, and reports that nothing changed. What went wrong, and where should the setting go?
Show answer
Cargo ignores [profile] sections in member crates and only reads them from the workspace root Cargo.toml, so the setting had no effect (Cargo also prints a warning). Move it to the root manifest. If only some packages need it, use [profile.dev.package.<name>] there.