Sub topic 1
Where the time goes: rustc, LLVM, and the linker
Prerequisites
This chapter assumes you know basic Rust syntax and ownership, and that you can build, run and test a project with Cargo. You should be comfortable reading a Cargo.toml and working on the command line. No compiler internals are required.
The build is a pipeline
When you run cargo build, Cargo decides which crates need compiling and starts one rustc process per crate. It starts them in dependency order, and it runs independent crates in parallel. Inside each rustc process, the work moves through several phases. After all crates are compiled, a linker joins the results into one binary.
Three phases account for most of the wall-clock time:
- The front end (rustc). It parses your code, expands macros, resolves names, checks types and borrows, and lowers the code to MIR, rustc's mid-level intermediate representation.
- Code generation (LLVM). rustc translates MIR into LLVM IR and hands it to LLVM. LLVM is a separate compiler back end that optimizes the IR and emits machine code.
- Linking. A linker, such as
ld,lldormold, combines the object files and libraries into the final executable.
Which phase usually dominates
The answer depends on the profile and the project.
| Situation | Usual bottleneck | Why |
|---|---|---|
| Debug build, small change | Linking, then LLVM | The front end is incremental, but the linker re-reads everything. |
| Release build | LLVM | Optimization passes run on every function. |
| Check only (`cargo check`) | Front end | LLVM and the linker are skipped. |
| Many dependencies, clean build | Whole pipeline, repeated per crate | Each crate goes through every phase. |
In practice, LLVM is the largest cost in most full builds. rustc often generates a lot of IR, and LLVM then spends time optimizing and emitting it. The front end matters most when your code leans heavily on macros, traits and generics. The linker matters most in the edit-compile loop of a large binary, where a one-line change still forces a full link.
Why your own code can make LLVM slower
Generic functions are compiled separately for each concrete type they are used with. This is called monomorphization. A generic function used with ten types produces ten copies of machine code, and LLVM must process all of them.
fn largest<T: PartialOrd + Copy>(items: &[T]) -> T {
let mut max = items[0];
for &item in items {
if item > max {
max = item;
}
}
max
}
fn main() {
println!("{}", largest(&[3, 9, 4]));
println!("{}", largest(&[1.5, 0.2]));
println!("{}", largest(&['a', 'z', 'q']));
}This program compiles largest three times: for i32, f64 and char. That is cheap here. In a large codebase, with many generic functions and many types, it adds up. A later chapter covers how to reduce this cost.
What this means for the rest of the tutorial
Each technique in this tutorial targets one phase.
- Faster feedback loops, such as
cargo check, skip LLVM and linking. - Profile settings reduce the work LLVM and the linker do.
- Trimming dependencies means fewer crates pass through the whole pipeline.
- Faster linkers replace the last step.
- Workspaces and caching reduce how much is rebuilt.
The next chapter shows how to measure a real build, so you can see which phase costs you the most.
Exercise
You change one line in a large application and run cargo build in debug mode. The compile step is quick, but the command still takes many seconds. Which phase is the most likely cause, and why?
Show answer
The linker. Incremental compilation limits the front end and LLVM work to the changed parts, but the linker must still combine all object files and libraries into a new binary on every build. You should confirm this by measuring, which the next chapter covers.
Exercise
A generic function is called with 20 different concrete types. How many compiled copies of it does LLVM have to process, and which phase pays the cost?
Show answer
Twenty copies, one per type, because of monomorphization. The cost falls on LLVM code generation, which optimizes and emits machine code for each copy.