As lead of a Rust service team, which Cargo release profile would you standardise — LTO, codegen-units, panic strategy, debug info — and why?
answer
- local speed versus shipped artifact
- a custom profile that inherits
- backtraces you can read in production
- unwind or abort is a behaviour choice
basics
~20 sKeep release near its defaults for local builds and ship from a custom profile inheriting release: LTO and codegen-units = 1 chosen by measurement, debug = "line-tables-only" with split debug info, and panic left at unwind unless abort is deliberate.
solid answer
~40 sThere is no single right profile, so I would decide per concern and write it in the root manifest. Local `--release` builds stay near the defaults so developers iterate quickly. CI builds shipped artifacts with a custom profile, say `[profile.dist]` with `inherits = "release"`. In it: `lto = "thin"`, or `"fat"` where benchmarks show it pays, plus `codegen-units = 1`, accepting longer CI builds. `debug = "line-tables-only"` gives file-and-line backtraces without full debug info, and `split-debuginfo = "packed"` keeps that info beside the binary, not in it. I would avoid `strip = true`, which removes the names crash reports need. `panic` stays `"unwind"` unless the service is built to die on panic and be restarted. I would benchmark the shipped profile and revisit it when the toolchain changes.
code
toml · 6 lines[profile.dist]
inherits = "release"
lto = "thin"
codegen-units = 1
debug = "line-tables-only"
split-debuginfo = "packed"go deeper
Recall the release defaults and that a custom profile with inherits = "release" can hold different settings, selected with --profile.
Explain what lto, codegen-units, panic, debug and split-debuginfo each change in the shipped binary and in build time.
Show the operational side: readable production backtraces through line tables, why strip = true hurts crash reports, and that tests never exercise panic = "abort".
Own the trade-off between CI minutes, runtime speed, binary size and diagnosability; separate local and shipping profiles, benchmark what you ship and revisit on toolchain upgrades.
## The decision a lead actually owns A **profile** is the set of compiler settings Cargo uses for a build, and the team's release profile decides how fast the shipped binary runs, how long CI takes to build it, how big it is, and what a crash in production leaves behind for whoever is on call. Cargo's defaults for `release` (`opt-level = 3`, `lto = false`, `codegen-units = 16`, `panic = "unwind"`, `debug = false`) are a reasonable compromise, but they serve no team's priorities exactly. The Cargo book recommends experimenting, warns that results can surprise, and advises re-evaluating settings as `rustc` changes — so the profile is a policy to own, not a snippet to copy. ## Two profiles, two jobs The cleanest structure separates local iteration from shipping: - `release` stays close to the defaults, so `cargo build --release` on a laptop finishes quickly. - A **custom profile**, for example `[profile.dist]`, holds the expensive settings. A custom profile must declare `inherits`; without it Cargo stops with `` profile `dist` is missing an `inherits` directive ``. It is selected with `--profile dist`, and its output lands in `target/dist`. - CI builds shipped artifacts with `cargo build --profile dist`; Cargo rejects a command that also passes `--release`. ## The settings and their trade-offs | concern | options | trade-off | |---|---|---| | cross-crate optimisation | `lto = "thin"` or `true` (fat) | fat can win a little more runtime speed and size; thin is far cheaper to link | | codegen units | `codegen-units = 1` | better code; a slower, less parallel compile | | panic strategy | `"unwind"` or `"abort"` | abort is smaller and simpler; any panic ends the process | | debug info | `debug = "line-tables-only"` | file and line in backtraces; no variable or type info | | where it lives | `split-debuginfo = "packed"` | debug info beside the binary, shipped or archived separately | | stripping | `strip = "none"`, `"debuginfo"` or `"symbols"` | `"symbols"` removes the names crash reporting needs | | overflow | `overflow-checks = true` | panics instead of silent wrapping, at a small cost | ## Debug info is an operations decision Measured on one macOS build, a panic with `RUST_BACKTRACE=1` showed three different pictures: 1. **Default release** (debug info off, so Cargo strips debuginfo by default): function names, no files or lines. 2. **`strip = true`**: no frames at all, only the panic message. 3. **`debug = "line-tables-only"`**: function names with file and line for each frame, and `split-debuginfo = "packed"` put the debug info in a `.dSYM` bundle next to the binary. For a service, option 3 is usually worth its cost: it is the minimum debug info that yields file-and-line backtraces. The rustc documentation itself advises programs that want crash reporting to avoid `-C strip=symbols`. The debugger-side handling of that info belongs to the debugger lessons; the profile decides what exists to read. ## Panic strategy is a behaviour decision - With `"unwind"`, a panic unwinds the stack; a server framework can catch a panicking request handler and keep serving others. What catching a panic means in code is the language's topic. - With `"abort"`, the binary is smaller and has no unwinding paths, but any panic ends the process. That suits a process that is supervised and restarted, and it must be a conscious choice. - Tests, benchmarks, build scripts and proc macros **ignore** the `panic` setting, so the test suite never exercises abort behaviour. `cargo test --release` builds with the release profile's `lto` and `codegen-units` but still unwinds. ## Governing the choice - **Benchmark what you ship.** `cargo bench` uses the `bench` profile, which inherits `release`, not your custom profile; run `cargo bench --profile dist`, or make sure the two do not drift. - **Budget CI time.** Fat LTO with one codegen unit can multiply build minutes; measure before and after, and keep the expensive profile off the pull-request loop. - **Write it down once.** Profiles live only in the workspace root manifest, and config files or `CARGO_PROFILE_*` variables can override them, so state in one place which source CI uses. - **Revisit on toolchain upgrades.** A new LLVM can change which setting wins.
- Why put the expensive settings in a custom profile rather than in `[profile.release]`?Developers run `cargo build --release` constantly, and fat LTO with one codegen unit can make that minutes slower. A custom profile with `inherits = "release"` keeps local release builds fast, gives CI one named profile for shipped artifacts, and writes its output to its own `target/<name>` directory so the two never mix.
- If the team picks `panic = "abort"`, what does the test suite tell you about it?Little. Tests, benchmarks, build scripts and proc macros ignore the `panic` setting, and the test harness needs unwinding, so even `cargo test --release` builds tests that unwind. Abort behaviour has to be checked by running the real binary under its supervisor.
- When would you choose fat LTO over thin LTO for the shipping profile?When a benchmark of the real workload shows fat LTO measurably faster or smaller and the extra link time fits the CI budget. The rustc docs describe thin LTO as reaching performance gains similar to fat at far lower cost, and note it can even beat fat on very large projects, so thin is the cheaper default to beat.
- The fastest release profile is the same for every project.
- A custom profile starts from the dev settings unless told otherwise.
- strip = true costs nothing, since panic messages keep file and line.
- Tests built with --release run with the release panic strategy.
- cargo bench automatically uses whatever profile CI ships with.
- Full debug info is the only way to get file and line in backtraces.