Skip to content

Lesson 14 of 14 · 18m

Release Builds and Performance

What --release actually changes, lto and codegen-units, the musl allocator cliff and mimalloc, cargo-chef Docker caching, and profiling with flamegraph and cargo bloat.

cargo build and cargo build --release are not the same binary with a different name. The debug build compiles with opt-level = 0, overflow checks on, and debug_assert! live — it's the binary where assert!(x < len) is a real check and integer overflow panics. The release binary flips all of that off. If your perf numbers, your panic behavior, or your binary size were measured on the debug build, you measured a different program than the one you deploy.

[profile.release]: the knobs that matter

[profile.release]
lto = "fat"            # cross-crate inlining; "thin" is the faster-compiling middle
codegen-units = 1      # one LLVM unit → max optimization, slowest compile (default 16)
strip = "symbols"      # remove the symbol table → smaller, harder to debug in prod
panic = "abort"        # or "unwind" — see the hardening lesson
opt-level = 3          # "s"/"z" trade speed for size on constrained targets

lto and codegen-units = 1 are the two that move real numbers on a hot service — typically single-digit to low-double-digit percent on CPU-bound paths — at the cost of compile time. strip = "symbols" shrinks the artifact but takes your backtraces with it; the middle ground is strip = "debuginfo" or keeping debug = true and splitting symbols for your symbol store. Set these at the workspace root — profiles only read from the root manifest.

The musl allocator cliff — and mimalloc/jemalloc

This one bites teams on Alpine/musl images. glibc's malloc is tuned; musl's stock allocator is famously poor under multithreaded load — Tokio benchmarks on musl routinely show double-digit regressions that vanish when you swap allocators. It's one line and one dependency:

#[global_allocator]
static ALLOC: mimalloc::MiMalloc = mimalloc::MiMalloc;

mimalloc and tikv-jemallocator both ship as #[global_allocator] drop-ins. If your deploy target is x86_64-unknown-linux-musl for the static binary, switching the allocator is often the largest single-line performance win available. If you're on glibc, the gain is smaller but still real on allocation-heavy services — measure before and after, because allocator choice is a benchmark question, not a tribal-knowledge one.

Docker builds: cargo-chef is the cache you're missing

The naive Dockerfile does COPY . . && cargo build --release — every source change invalidates the dependency-build layer, and CI rebuilds tokio from scratch every push. cargo-chef splits the build: a recipe.json captures just the dependency graph, dependencies compile in one cached layer, and your source compiles in another:

FROM rust:1 AS chef
RUN cargo install cargo-chef
WORKDIR /app

FROM chef AS planner
COPY . .
RUN cargo chef prepare --recipe-path recipe.json

FROM chef AS builder
COPY --from=planner /app/recipe.json recipe.json
RUN cargo chef cook --release --recipe-path recipe.json
COPY . .
RUN cargo build --release

On a cached CI, dependency builds drop from minutes to zero. That's the difference between a 9-minute and a 90-second pipeline on a service with a real dependency tree.

Measuring: flamegraph, samply, cargo bloat

Performance claims without measurement are vibes. The toolchain:

  • cargo flamegraph — perf-based sampling profiler; gives you the flame graph showing where the CPU actually is, which is never where the code review guessed.
  • samply — the modern cross-platform sampler that opens in the Firefox Profiler; better for when perf isn't available or you want a shareable trace.
  • cargo bloat — what in the binary is big: which crates, which functions, how much of the artifact is tokio you didn't need. Pair with cargo llvm-lines to see which generic instantiations monomorphized into the most code — that's the impl Trait everywhere tax made visible.

The workflow is always the same: profile first, fix the thing the profile points at, re-measure. The failure mode is "optimize the thing that looks slow" — which is usually a visible .clone() while the actual cost is an O(n²) in a serializer nobody looked at.

The bug that ships

The release bug that ships is debug_assertions doing load-bearing work: a debug_assert! checking an invariant that "should always hold," silently removed in --release — so the invariant stops being enforced exactly when the binary gets fast. If a check must run in production, it's an assert! or an if + return Err, not a debug_assert!. The debug/release gap is the feature; the trap is forgetting it exists.

Try this

Take a service's Cargo.toml, add lto = "thin" and codegen-units = 1 to [profile.release], rebuild, and compare wrk numbers. Then run cargo bloat --release -n 20 and look at which crate is the top entry — if it's tokio's full feature on a binary that doesn't need it, that's the feature-unification bug from the workspace lesson made measurable.

Field notes

  • cargo-pgo automates profile-guided optimization — -Cprofile-generate/-Cprofile-use — for another few percent on CPU-bound services; BOLT goes further on top of that.
  • opt-level = "z" shrinks binaries meaningfully for edge/embedded targets; on a server, 3 is almost always right.
  • Release builds remove debug_assert! and overflow checks; if overflow must panic in prod, overflow-checks = true under [profile.release] is an explicit choice.

Was this lesson helpful?

Sign in to rate

Keep reading

Related articles from the SudoSecurity blog.