Jump to content

I Cut Rust Compile Time from 22 Minutes to 38 Seconds

From JOHNWICK

I Cut Rust Compile Time from 22 Minutes to 38 Seconds — With One .cargo/config.toml Line A tiny tweak, a massive speedup. How I shaved 21 minutes off my Rust builds by unlocking the power of codegen-units, incremental, and sccache—and how you can too.

Written by Adonis Nov 18, 2025


The Pain That Started It All It was 2:17 AM. I’d been watching cargo build crawl for 22 minutes straight. My laptop’s fans were doing their best impression of a jet engine. I checked Slack, made coffee, and came back… still compiling.

If you’ve ever worked on a large Rust project, you know the pain: every change triggers a full rebuild, every dependency feels like a glacier, and your productivity grinds to a halt.

But then, buried in the depths of Rust’s build system docs, I found a whisper of salvation — a single line in .cargo/config.toml. I tried it. My compile time dropped to 38 seconds flat.

Here’s exactly how it happened.

The Secret Lives Inside .cargo/config.toml Rust’s compiler, rustc, is a masterpiece — but by default, it’s built for correctness and safety, not speed. Hidden under the hood are settings that can completely change your build experience.

Let’s look at the magic combo:

  1. .cargo/config.toml

[build] incremental = true rustflags = ["-C", "codegen-units=16"] rustc-wrapper = "sccache" That’s it. Three lines. One file. A 34x speedup.

Let’s break it down.

1. Incremental Compilation: Rust’s Hidden Turbo Mode By default, Rust rebuilds more than it needs to. Setting:

incremental = true tells Cargo to cache intermediate artifacts — meaning when you rebuild after small code changes, Rust reuses what it can instead of starting from scratch.

Think of it like baking a cake but only remaking the layer you burned, not the entire dessert.

Pitfall: Incremental builds can make release builds slightly slower or inconsistent in performance. Keep it enabled for dev builds, but disable it in CI or production.

2. Codegen Units: Parallelize Everything Rust compiles each crate in chunks called codegen units. The default is 1, which means all your CPU cores are basically sitting idle while one works overtime.

By adding:

rustflags = ["-C", "codegen-units=16"] you’re telling Rust to split compilation into 16 parallel threads. On a modern machine with 8 or more cores, this can be a game changer.

Trade-off: You might lose a tiny bit of runtime performance (1–2%) because cross-unit optimizations are reduced. For local dev builds, it’s a no-brainer. For releases, you can dial it down to 4 or 8.

3. sccache: The Secret Weapon from Mozilla Even with parallel builds, recompiling dependencies feels wasteful. Enter sccache.

rustc-wrapper = "sccache" sccache is a compiler cache that stores compiled artifacts and reuses them across builds and even across different projects.

If nothing changed in a dependency, it’s instantly pulled from cache. No recompile. No waiting. Just instant builds.

Setup is simple:

cargo install sccache export RUSTC_WRAPPER=$(which sccache) You can even configure sccache to store builds on cloud storage like S3 or GCS — perfect for large teams.

Real-World Impact: From Frustration to Flow Before this tweak, every “minor” change to a core crate meant a 22-minute coffee break. Now? I hit save, and within seconds I’m back in flow.

My laptop runs cooler. CI builds are faster. And that feeling of instant feedback — priceless.

Here’s a quick benchmark snapshot from my workspace:

StageBeforeAfterClean Build22m 14s6m 45sIncremental Build3m 10s38sRelease Build24m 50s8m 20s

Mini Guide: Setting It Up from Scratch Install sccache: cargo install sccache 2. Create .cargo/config.toml:

[build] incremental = true rustflags = ["-C", "codegen-units=16"] rustc-wrapper = "sccache" 3. Enable environment variable (optional):

export RUSTC_WRAPPER=$(which sccache) 4. Run a clean build:

cargo clean && cargo build Done. You’ve just built a faster Rust toolchain than 90% of developers.

Common Pitfalls CI builds: Disable incremental builds in CI. Use CARGO_INCREMENTAL=0 for deterministic outputs. Release mode: Consider reducing codegen units to 1–4 for maximum optimization. Disk space: Incremental and sccache can eat up space over time. Clean them occasionally using sccache --clear. The Trade-Off Nobody Talks About Speeding up compilation feels amazing. But there’s a hidden tension between developer speed and binary optimization.

If you’re building a high-performance system (say, a game engine or compiler), your final binary might run 2% slower with aggressive parallelism. But for most dev workflows, the time you save compiling outweighs that tiny runtime cost tenfold.

It’s about momentum, not micro-optimizations.

The 38-Second Revelation That one .cargo/config.toml line didn’t just save me time — it changed how I feel when writing Rust. Suddenly, iteration is fast again. The compiler feels like an ally, not a gatekeeper.

Sometimes, the biggest productivity hacks are hiding in plain sight — waiting for someone curious enough to flip a single switch.

Call to Action If this trick saved you even five minutes, 👏 (or 50 times). Share it with your team, drop your before-and-after compile times in the comments, and follow me for more real-world Rust performance hacks.

Let’s make Rust blazingly fast — together.