Release profiles¶
Level: 201 · working knowledge
One line: --release is not one switch but a profile — opt-level, debug, lto, codegen-units, panic, strip — and the defaults are a compromise you can move toward a faster build, a faster binary or a smaller one.
Stub — an outline, not a lesson. There is no runnable example behind this page yet, so nothing on it has been through the check that backs every other claim in this library. The bullets below are the questions the finished page has to answer.
What it has to cover¶
- What
devandreleaseset by default, read from the Cargo reference rather than remembered opt-level0–3,"s"and"z";lto = "thin"versustrue;codegen-units = 1— each one measured on one program for build time, run time and sizepanic = "abort": a smaller binary and no unwinding — and what it takes away (catch_unwind,Dropon panic). Link what a panic costsdebug,split-debuginfoandstrip: why a release binary with symbols is useful and when to drop them- Custom profiles (
[profile.bench-fast] inherits = "release") and per-package overrides ([profile.dev.package."*"] opt-level = 3) for a fast dev loop with optimised dependencies overflow-checks: on indev, off inrelease— the setting behind signed overflow behaving differently in the two builds
The trap it exists for¶
Benchmarking a dev build. Unoptimised Rust can be tens of times slower than --release, and every iterator chain looks expensive. The opposite mistake is shipping overflow-checks = false without realising that dev was catching an overflow the release build now wraps silently.
Where this sits¶
Compile times is about making the build faster. This page is about what the build produces. What the optimizer does shows what opt-level changes in the machine code.
See also¶
- Compile times — the other side of the trade
- What the optimizer does — what
opt-levelbuys, in instructions black_boxis a hint — benchmarking an optimised build honestly- What a panic costs — what
panic = "abort"removes - Signed overflow —
overflow-checksand the C behaviour it replaces - Production template — a profile set chosen for code that ships
If you are coming from another language¶
- C and C++.
-O0/-O2/-Os,-flto,-gandstripmap almost one to one. The difference is that Cargo names the bundle, so a project cannot drift into a different combination per file. The C++ library's the optimizer deletes your benchmark ↗ is the benchmarking half. - Go. Go has no release profile:
go buildalways optimises, and-ldflags="-s -w"is the nearest thing tostrip.
Po polsku¶
--release to nie jeden przełącznik, tylko profil: opt-level, debug, lto, codegen-units, panic, strip i overflow-checks. Najdroższa pomyłka to mierzenie wydajności na kompilacji dev; druga w kolejności to nieświadomość, że overflow-checks jest włączone tylko w dev, więc przepełnienie złapane podczas testów w wersji wydanej po cichu się zawinie.
Szukaj po polsku: profil kompilacji release · optymalizacja w Cargo · cargo profile release lto codegen-units · rust smaller binary size