Skip to content
rusthacks.dev
Performance

Release Mode Versus Debug Mode in Rust and the Speed Gap

cargo build is slow to run and fast to compile, cargo build --release is the reverse. See what each profile changes, how to measure the gap and how to tune the dev profile.

2 min read
Summarize in AI
Flat illustration for the post “Release Mode Versus Debug Mode in Rust and the Speed Gap”, composed around perf.

A test that took two seconds in debug takes forty milliseconds in release, and the first thing to learn about Rust performance is to not judge either by the other. The two builds are the same compiler with different profile settings, and the settings matter more than they do in most languages because Rust leans on the optimizer to remove abstraction. This post lists what differs, how to measure the gap yourself and how to make debug usable. It extends Rust performance basics.

Rust 1.90.

What the profiles set

Settingdevrelease
opt-level03
debug-assertionsonoff
overflow-checksonoff
debugfullnone
incrementalonoff
codegen-units25616
ltooffoff

With opt-level = 0 the compiler does almost no inlining, so an iterator chain becomes a series of real function calls. At opt-level = 3 the same chain often compiles to a tight loop.

Measure the gap

rust
fn main() {
    let start = std::time::Instant::now();
    let sum: u64 = (0..200_000_000u64).filter(|n| n % 3 == 0).map(|n| n * 2).sum();
    println!("{sum} in {:?}", start.elapsed());
}
Shell
cargo run
cargo run --release

The output has the same sum in both, and a much smaller time in release. We do not give numbers because the ratio depends on the CPU; run it and compare, then try the same with a loop that does file I/O to see a smaller gap. The release run may constant-fold or vectorize the loop, which is why a toy example overstates the difference for real programs.

Behavior differences

rust
fn main() {
    let x: u8 = 250;
    let y = x + std::hint::black_box(10);
    println!("{y}");
}

In debug this panics with attempt to add with overflow. In release it prints 4, because overflow wraps. If your code depends on overflow behavior, use checked_add, wrapping_add or saturating_add explicitly. More on that in integer overflow bugs in release builds. debug_assert! also disappears in release.

Make debug workable

Cargo.tomlTOML
[profile.dev]
opt-level = 1

[profile.dev.package."*"]
opt-level = 3

This optimizes all dependencies at the highest level (they rarely change, so it is a one-time cost) and your own code lightly. Many projects find parsing, hashing and serialization crates unusable at opt-level = 0. You keep debug info and fast rebuilds for your own crate.

Release options that matter

lto = "thin", codegen-units = 1 and panic = "abort" trade compile time for speed and size; see release profile settings. Add debug = "line-tables-only" to keep readable profiles (profiling guide).

Limits

Do not deploy a debug build. Do not benchmark one. Do not conclude that Rust is slow from one. For the profile reference, see the Cargo book's chapter on profiles.

Taggedprofiles
Share

Keep reading

Flat illustration for the post “Stale While Revalidate Caching in Rust Services Explained”, composed around perf.
Performance

Stale While Revalidate Caching in Rust Services Explained

Serve the cached value immediately, even if slightly old, and refresh it in the background. Learn the idea, the HTTP Cache-Control directives and an in-process implementation with a freshness window and a grace window.

4 min read

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.