Kron's

Speeding Up Rust Compilation

Rust compile times are slow. Here's why and what you can do about it.

The Problem

Rust compilation has two bottlenecks:

  1. Frontend (rustc): parsing, macro expansion, type checking, borrow checking
  2. Backend (LLVM): optimization, code generation

If cargo check is slow, your bottleneck is the frontend. If cargo check is fast but cargo build is slow, it's the backend.

Backend Fixes

Cranelift

Cranelift is an alternative code generator built for the Wasmtime WebAssembly runtime. It prioritizes compile speed over output quality - perfect for dev builds.

1rustup component add rustc-codegen-cranelift-preview
2CARGO_PROFILE_DEV_CODEGEN_BACKEND=cranelift cargo build

Expect ~2x faster codegen with slower runtime performance.

mold Linker

The default linker is slow. mold is a drop-in replacement optimized for speed.

1sudo apt install mold clang
1# .cargo/config.toml
2[target.x86_64-unknown-linux-gnu]
3linker = "clang"
4rustflags = ["-C", "link-arg=-fuse-ld=mold"]

Linking goes 2-5x faster.

sccache

Caches compiler outputs. Helps with rebuilds and CI.

1cargo install sccache
1# .cargo/config.toml
2[build]
3rustc-wrapper = "sccache"

The cache lives on disk by default. The OS page cache keeps hot entries in RAM automatically - no need for tmpfs unless you're seeing disk I/O bottlenecks.

Aggressive Dev Profile

 1# Cargo.toml
 2[profile.dev]
 3opt-level = 0
 4debug = false
 5codegen-units = 256
 6incremental = true
 7
 8[profile.dev.package."*"]
 9opt-level = 0
10codegen-units = 256

More codegen units = more parallelism = faster builds (but worse code).

Frontend Fixes

Bad news: there's no quick fix for frontend speed.

The Rust compiler team is working on parallel frontend compilation, but it's years away from matching Go's speed. The borrow checker, complex generics, and procedural macros all take time.

What helps:

Why Go is Faster

Go was designed from day one for fast compilation:

Rust chose safety and zero-cost abstractions over compile speed. That's a fundamental design tradeoff, not a bug to fix.

Complete Setup

 1# Cargo.toml
 2[profile.dev]
 3opt-level = 0
 4debug = false
 5codegen-units = 256
 6incremental = true
 7
 8[profile.dev.package."*"]
 9opt-level = 0
10codegen-units = 256
1# .cargo/config.toml
2[target.x86_64-unknown-linux-gnu]
3linker = "clang"
4rustflags = ["-C", "link-arg=-fuse-ld=mold"]
5
6[build]
7rustc-wrapper = "sccache"
1# Install tools
2sudo apt install mold clang
3cargo install sccache
4rustup component add rustc-codegen-cranelift-preview

Reality Check

Even with all optimizations, Rust won't match Go's compile speed. But you can get 2-4x faster dev builds, which makes iteration less painful.

Ship with LLVM. Iterate with cranelift + sccache + mold.

#rust #performance #compilation