Slash Rust Build Times by 21 Minutes Using Incremental Builds, Codegen Units, and sccache

By tweaking Cargo's configuration to enable incremental compilation, increase codegen units, and employ sccache as a compiler cache, the author reduced a 22‑minute Rust build to under a minute, dramatically speeding up development feedback loops.

DevOps Coach
DevOps Coach
DevOps Coach
Slash Rust Build Times by 21 Minutes Using Incremental Builds, Codegen Units, and sccache

When a routine cargo build stalls for 22 minutes, the author discovered a three‑pronged configuration tweak that collapsed the compile time to 38 seconds.

The hidden settings in .cargo/config.toml

# .cargo/config.toml
[build]
incremental = true
rustflags = ["-C", "codegen-units=16"]
rustc-wrapper = "sccache"

These three lines enable incremental builds, parallelize compilation across 16 codegen units, and cache compiled artifacts with sccache.

1. Incremental compilation

Setting incremental = true tells Cargo to cache intermediate results, so small code changes reuse previous work instead of rebuilding everything. This speeds up iterative development but may slightly slow release builds, so it is recommended only for local development.

2. Codegen units

Rust defaults to a single codegen unit, leaving extra CPU cores idle. Adding rustflags = ["-C", "codegen-units=16"] splits the compilation into 16 parallel threads, yielding massive speed gains on multi‑core machines. The trade‑off is a modest 1–2% runtime performance loss, which can be mitigated by lowering the unit count for release builds.

3. sccache – a compiler cache

sccache

stores compiled artifacts and reuses them across builds and even different projects. When a dependency hasn't changed, the cached result is fetched instantly, eliminating unnecessary recompilation.

# In your ~/.cargo/config.toml
[build]
rustc-wrapper = "sccache"

Optionally, sccache can be configured to store its cache in cloud storage (S3, GCS) for large teams.

Real‑world impact

Before the tweaks, a tiny change to a core crate required a 22‑minute wait. After applying the configuration, the same change rebuilt in a few seconds, lowering laptop temperature, speeding CI pipelines, and restoring a smooth development flow.

Mini guide: Getting started from scratch

Install sccache : cargo install sccache Create ~/.cargo/config.toml with the three lines shown above.

Optional environment variable : export RUSTC_WRAPPER="sccache" Run a clean build :

cargo clean && cargo build

Common pitfalls

CI builds : Disable incremental builds in CI (e.g., CARGO_INCREMENTAL=0) for deterministic output.

Release mode : Consider reducing codegen units to 1–4 to maximize runtime performance.

Disk space : Incremental builds and sccache can consume significant storage; clean periodically with cargo clean and sccache --zero-stats.

Unspoken trade‑offs

While the speed boost is valuable, aggressive parallelism may increase binary size or slightly reduce runtime speed (≈2%). For most development workflows the productivity gain outweighs this minor cost, but performance‑critical projects (e.g., game engines) should tune settings carefully.

Takeaway

The simple .cargo/config.toml tweak not only saves minutes per build but also transforms the developer experience, turning the compiler from a bottleneck into a supportive ally.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

performanceBuild Optimizationincremental compilationbackend-developmentcodegen-unitssccache
DevOps Coach
Written by

DevOps Coach

Master DevOps precisely and progressively.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.