Sub topic 2
Measure first: cargo build --timings and friends
This chapter assumes you can build, run and test a Rust project with Cargo, and that you have read how a build splits into rustc, LLVM and the linker. Before changing any setting, find out where your own build spends its time. A fix aimed at the wrong phase costs effort and changes nothing.
Three questions, three tools
Each tool answers a different question about a slow build.
| Question | Tool |
|---|---|
| Which crates take longest, and what waits on what? | `cargo build --timings` |
| Which generic functions produce the most code? | `cargo llvm-lines` |
| Which dependencies and features are pulled in? | `cargo tree -e features` |
# Per-crate timeline written to target/cargo-timings/cargo-timing.html
cargo build --timings
# Which generic functions produce the most LLVM IR (requires: cargo install cargo-llvm-lines)
cargo llvm-lines | head -20
# Which dependencies pull in which features and crates
cargo tree -e featuresReading the timings report
Run cargo build --timings from a clean state, for example after cargo clean. Open the HTML file it writes in a browser. Cargo also keeps a copy with a date in its name, so you can compare reports over time.
The report has two main parts:
- A chart with one bar per crate. The bar's length is the time spent compiling that crate. Bars that start late show that the crate had to wait for its dependencies.
- A table listing each crate with its total time, and the time spent in code generation when that is reported.
Look for these patterns:
- One long bar that many other bars wait for. This crate is on the critical path. Speeding it up, or splitting it, shortens the whole build.
- A long bar at the very end. This is usually your own top-level crate. Check whether its time goes to type checking or to code generation.
- Many short bars running side by side. The build is already parallel, and more cores will help.
- A build script or proc-macro crate early in the chart. Everything that uses it must wait until it has compiled and run.
In this example, the app crate cannot start until everything before it finishes. Parallel jobs on other crates do not shorten that chain.
Finding code bloat with cargo llvm-lines
If the timeline shows one crate spending most of its time in code generation, run cargo llvm-lines. It lists functions by how many lines of LLVM IR they produce. Generic functions often appear many times, one copy for each type they are used with. A function with a large line count and many copies is a candidate for the technique in the monomorphization chapter.
Checking features with cargo tree
cargo tree -e features shows which features are switched on for each dependency, and which crate switched them on. Use it when a dependency compiles far more code than you expected. A common cause is a default feature that you do not use.
Keep a baseline
Write down the numbers before you change anything. Keep the timing report and the wall-clock time of cargo build. After each change, measure again and compare. If the number does not move, undo the change. Several small settings can also cancel each other out, so change one thing at a time.
Exercise
A timings report shows one proc-macro crate with a long bar at the start. Every other bar begins after it ends. What does this tell you, and which command would you run next to see why that crate is in your build?
Show answer
The proc-macro crate is on the critical path, because every crate that depends on it must wait for it to compile. Run cargo tree -e features to see which dependency pulls it in and which features enable it. You may then be able to drop a feature or a dependency that you do not need.
Wait, I need to correct a mistake: the chapter above contains a stray empty diagram fence and uses fence labels that do not match the required form. Here is the corrected chapter.
Measure first: cargo build --timings and friends
This chapter assumes you can build, run and test a Rust project with Cargo, and that you know a build splits into rustc, LLVM and the linker. Before changing any setting, find out where your own build spends its time. A fix aimed at the wrong phase costs effort and changes nothing.
Three questions, three tools
Each tool answers a different question about a slow build.
| Question | Tool |
|---|---|
| Which crates take longest, and what waits on what? | `cargo build --timings` |
| Which generic functions produce the most code? | `cargo llvm-lines` |
| Which dependencies and features are pulled in? | `cargo tree -e features` |
# Per-crate timeline written to target/cargo-timings/cargo-timing.html
cargo build --timings
# Which generic functions produce the most LLVM IR (requires: cargo install cargo-llvm-lines)
cargo llvm-lines | head -20
# Which dependencies pull in which features and crates
cargo tree -e featuresReading the timings report
Run cargo build --timings from a clean state, for example after cargo clean. Open the HTML file it writes in a browser. Cargo also keeps a copy with a date in its name, so you can compare reports over time.
The report has two main parts:
- A chart with one bar per crate. The bar's length is the time spent compiling that crate. Bars that start late show that the crate had to wait for its dependencies.
- A table listing each crate with its total time, and the time spent in code generation when that is reported.
Look for these patterns:
- One long bar that many other bars wait for. This crate is on the critical path. Speeding it up, or splitting it, shortens the whole build.
- A long bar at the very end. This is usually your own top-level crate. Check whether its time goes to type checking or to code generation.
- Many short bars running side by side. The build is already parallel, and more cores will help.
- A build script or proc-macro crate early in the chart. Everything that uses it must wait until it has compiled and run.
Diagram: Flowchart where serde feeds a proc-macro crate, then the core crate, then the app crate, then linking, one step after another.
In this example, the app crate cannot start until everything before it finishes. Parallel jobs on other crates do not shorten that chain.
Finding code bloat with cargo llvm-lines
If the timeline shows one crate spending most of its time in code generation, run cargo llvm-lines. It lists functions by how many lines of LLVM IR they produce. Generic functions often appear many times, one copy for each type they are used with. A function with a large line count and many copies is a candidate for the technique in the monomorphization chapter.
Checking features with cargo tree
cargo tree -e features shows which features are switched on for each dependency, and which crate switched them on. Use it when a dependency compiles far more code than you expected. A common cause is a default feature that you do not use.
Keep a baseline
Write down the numbers before you change anything. Keep the timing report and the wall-clock time of cargo build. After each change, measure again and compare. If the number does not move, undo the change. Several small settings can also cancel each other out, so change one thing at a time.
Exercise
A timings report shows one proc-macro crate with a long bar at the start. Every other bar begins after it ends. What does this tell you, and which command would you run next to see why that crate is in your build?
Show answer
The proc-macro crate is on the critical path, because every crate that depends on it must wait for it to compile. Run cargo tree -e features to see which dependency pulls it in and which features enable it. You may then be able to drop a feature or a dependency that you do not need.