Sub topic 14
Release builds: codegen units, LTO and opt-level
This chapter assumes you can edit Cargo.toml, run cargo build --release, and read the timing output from earlier chapters. Release builds trade compile time for runtime speed. Three profile settings control most of that trade: opt-level, codegen-units and lto.
What each setting does
opt-levelsets how hard LLVM optimizes. Values are0(none),1,2,3(most speed),"s"and"z"(smaller code).codegen-unitssets how many pieces rustc splits a crate into. LLVM compiles the pieces in parallel. More pieces mean a faster build, but LLVM sees less code at once and may inline less.lto(link-time optimization) lets LLVM optimize across crate boundaries.falseruns a limited "thin local" pass within each crate."thin"and"fat"(ortrue) optimize across crates."fat"is the slowest and can give the best code.
Defaults
The release profile defaults to opt-level = 3, codegen-units = 16, lto = false and debug = false. Those defaults favor build speed over the fastest possible code, which is a reasonable starting point.
Two profiles for two goals
Do not tune one profile for everything. Keep release fairly quick to build for day-to-day testing, and add a custom profile for the final artifact.
# Cargo.toml
[profile.release]
opt-level = 3
codegen-units = 16
lto = false
# Used only for shipping: cargo build --profile dist
[profile.dist]
inherits = "release"
codegen-units = 1
lto = "fat"Run cargo build --profile dist when you need the best binary. The output goes to target/dist/, not target/release/.
Choosing values
| Goal | Suggested settings | Cost |
|---|---|---|
| Fastest release build | `codegen-units = 16` (default), `lto = false` | Slightly slower code |
| Balanced | `lto = "thin"` | Longer link phase, often a few percent faster code |
| Fastest runtime | `codegen-units = 1`, `lto = "fat"` | Slowest build, least parallelism |
| Smaller binary | `opt-level = "s"` or `"z"`, plus `lto` | Possibly slower code |
Setting codegen-units = 1 removes parallelism within a crate, so a large crate may take much longer. Measure before you commit to it.
Opt-level for dependencies
If your release build is slow because of heavy dependencies, lowering their optimization is possible but usually hurts runtime. A more common use is the reverse: keep your own crate at a low level in a profile used for tests, and optimize only the hot dependencies.
[profile.release.package.serde_json]
opt-level = 3
[profile.release.package."*"]
opt-level = 2A package-specific entry takes priority over the "*" entry, so serde_json stays at 3 while other dependencies use 2.
Panics and debug info
Two more settings reduce work for the linker and shrink the binary. panic = "abort" removes unwinding code, but panics will no longer unwind, so catch_unwind and destructors on panic stop working. strip = true removes symbols from the final file after linking.
[profile.dist]
inherits = "release"
panic = "abort"
strip = trueExercise
Your release binary is slightly slower than you want, and you are willing to wait longer for the final build only. Which profile changes would you make, and where would you put them so everyday cargo build --release stays fast?
Show answer
Add a custom profile such as [profile.dist] with inherits = "release", codegen-units = 1 and lto = "fat", and build it with cargo build --profile dist. The ordinary release profile keeps its defaults, so everyday release builds stay fast.
Exercise
Why can setting codegen-units = 1 make a build slower, even though it can produce faster code?
Show answer
With one codegen unit, LLVM compiles the whole crate as a single piece on one thread, so the parallelism is lost. LLVM can inline and optimize across the whole crate, which helps runtime speed, but the build takes longer.