| name | rust-build-times |
| description | Rust build time optimization skill for reducing slow compilation. Use when using cargo-timings to profile builds, configuring sccache for Rust, using the Cranelift backend, splitting workspaces for parallelism, choosing between thin LTO and fat LTO, or using the mold linker with Rust. Activates on queries about slow Rust compilation, cargo-timings, sccache Rust, cranelift backend, Rust workspace splitting, LTO tradeoffs, or mold linker with Rust. |
Rust Build Times
Purpose
Guide agents through diagnosing and improving Rust compilation speed: cargo-timings for build profiling, sccache for caching, the Cranelift codegen backend for faster dev builds, workspace crate splitting, LTO configuration trade-offs, and fast linkers (mold/lld).
Triggers
- "My Rust project takes too long to compile"
- "How do I profile which crates are slow to build?"
- "How do I set up sccache for Rust?"
- "What is the Cranelift backend and how does it help?"
- "Should I use thin LTO or fat LTO?"
- "How do I use the mold linker with Rust?"
Workflow
1. Diagnose with cargo-timings
cargo build --timings
cargo build --release --timings
cargo install cargo-llvm-lines
cargo llvm-lines --release | head -20
2. sccache — compilation caching for Rust
cargo install sccache
export RUSTC_WRAPPER=sccache
[build]
rustc-wrapper = "sccache"
sccache --show-stats
export SCCACHE_BUCKET=my-rust-cache
export SCCACHE_REGION=us-east-1
export AWS_ACCESS_KEY_ID=xxx
export AWS_SECRET_ACCESS_KEY=yyy
sccache --start-server
3. Cranelift codegen backend
Cranelift is a fast codegen backend (vs LLVM) — produces slower code but compiles much faster. Ideal for development builds:
rustup toolchain install nightly
rustup component add rustc-codegen-cranelift-preview --toolchain nightly
[unstable]
codegen-backend = true
[profile.dev]
codegen-backend = "cranelift"
CARGO_PROFILE_DEV_CODEGEN_BACKEND=cranelift \
RUSTFLAGS="-Zunstable-options" \
cargo +nightly build
Cranelift vs LLVM trade-off:
- Dev builds: 20–40% faster compilation with Cranelift
- Runtime performance: LLVM-compiled code is faster (Cranelift skips many optimizations)
- Release builds: always use LLVM
4. Workspace splitting for parallelism
A single large crate compiles sequentially. Split into smaller crates to enable Cargo parallelism:
[package]
name = "monolith"
[workspace]
members = [
"core",
"networking",
"ui",
"server",
"cli",
]
cargo tree | head -30
cargo tree --graph | dot -Tsvg > deps.svg
cargo build -j$(nproc) --timings
Rules for effective workspace splitting:
- Break circular dependencies first
- Separate proc-macros into their own crate (they block everything)
- Keep frequently-changed code isolated (less invalidation)
5. LTO configuration
LTO improves runtime performance but increases link time:
[profile.release]
lto = "thin"
codegen-units = 1
[profile.release-fast]
inherits = "release"
lto = "fat"
[profile.dev]
lto = "off"
codegen-units = 16
LTO comparison:
| Setting | Link time | Runtime perf | Use when |
|---|
lto = false | Fast | Baseline | Dev builds |
lto = "thin" | Moderate | +5–15% | Most release builds |
lto = "fat" | Slow | +15–30% | Maximum performance |
codegen-units = 1 | Slowest | Best | With LTO for release |
6. Fast linkers
The linker is often the bottleneck for large Rust projects:
sudo apt-get install mold
[target.x86_64-unknown-linux-gnu]
linker = "clang"
rustflags = ["-C", "link-arg=-fuse-ld=mold"]
cargo install cargo-zigbuild
cargo zigbuild --release
[target.x86_64-unknown-linux-gnu]
rustflags = ["-C", "link-arg=-fuse-ld=lld"]
[target.x86_64-apple-darwin]
rustflags = ["-C", "link-arg=-fuse-ld=/usr/local/bin/zld"]
Linker speed comparison (large project, typical):
- GNU ld: baseline
- lld: ~2× faster
- mold: ~5–10× faster
- gold: ~1.5× faster
7. Other quick wins
[profile.dev]
debug = 1
[profile.dev]
split-debuginfo = "unpacked"
CARGO_INCREMENTAL=0 cargo build
Related skills
- Use
skills/rust/cargo-workflows for Cargo workspace and profile configuration
- Use
skills/build-systems/build-acceleration for C/C++ equivalent build acceleration
- Use
skills/debuggers/dwarf-debug-format for debug info size/split-dwarf tradeoffs
- Use
skills/binaries/linkers-lto for LTO internals