Sub topic 2
Measure first: cargo build --timings and friends
Changing build settings without measuring is guesswork. A setting that helps one project can do nothing for another, because the slow part is different. This chapter covers the tools that show where time goes, so later chapters can fix the right thing.
This chapter assumes you can build and test a Rust project with Cargo, edit Cargo.toml, and use a terminal. It also assumes you know the three phases from the earlier chapter: rustc front end, LLVM code generation, and linking.
Decide what you are measuring
"The build is slow" describes at least three different situations. Each has a different cause and a different fix.
| Scenario | How to trigger it | What it tells you |
|---|---|---|
| Clean build | `cargo clean`, then build | Total cost of dependencies plus your code. This matters for CI and for new checkouts. |
| Incremental rebuild | Change one function body, then build | What you wait for in the edit loop. This is usually what hurts most. |
| No-op build | Build twice with no changes | Cargo overhead, build scripts that always rerun, and fingerprint problems. |
Pick the scenario that matches your complaint before you pick a tool. A clean build of 3 minutes is irrelevant if developers only rebuild after small edits and wait 40 seconds on linking.
cargo build --timings
Run this on a clean build first:
cargo clean
cargo build --timingsCargo writes an HTML report to target/cargo-timings/cargo-timing.html. It also keeps a copy with a timestamp in the file name, such as cargo-timing-20261202T140558Z.html, so you can compare runs. Open the file in a browser.
The flag works with other commands too. Use cargo build --release --timings for release builds, cargo check --timings for the check loop, and cargo test --no-run --timings for test builds.
Reading the report
The report has three parts worth knowing.
- Summary. It shows the total time, the number of units compiled, and the maximum concurrency reached. A unit is one thing Cargo asks rustc to do, such as compiling a library, a binary, a test target, or running a build script. One crate can produce several units, for example the library and its tests.
- Unit graph chart. Each unit is a horizontal bar on a time axis. The bar's start shows when the unit began. Its length shows how long it took. Part of each bar is drawn in a different shade to show code generation (the LLVM part). The rest of the bar is the front end.
- Concurrency chart. It plots how many units were running, waiting, and inactive over time. Low "active" counts on a machine with many cores mean the build is serialized by dependencies.
Below the charts is a table with the columns Unit, Total, Codegen, and Features. Sort it by Total to find the slowest units. The Features column lists the features enabled on that unit, which is useful in the next step.
What the shapes mean
- Many long bars early on, then a quiet tail. Third-party dependencies dominate. Look at the Features column and at proc-macro crates such as
syn. Later chapters cover feature trimming and macro cost. - One very long bar that others wait on. A single crate sits on the critical path. Everything that depends on it starts late. Splitting it or reducing what it depends on can help.
- A long final bar for your binary. Check how much of it is codegen. If the bar is mostly codegen, look at monomorphization. If the build is nearly finished and the last step is slow, run
cargo build -vand see whether the linker command takes most of the time. - A build script bar early and long. A
build.rsblocks every crate that depends on its output. It also reruns when its inputs change. - Low concurrency for most of the build. You have a dependency chain, not a lack of cores. More cores will not help.
Cargo pipelines compilation. A downstream crate can start type-checking as soon as the upstream crate's metadata (the .rmeta file) is ready, without waiting for its machine code. So a bar's end time is not always when dependents could start. Do not read the chart as strict "A finished, then B started".
Looking inside your own crate with cargo llvm-lines
--timings tells you which crate is slow. To find out why, look at how much code it generates. cargo-llvm-lines counts the lines of LLVM IR (the intermediate code that LLVM turns into machine code) per function.
# 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 featuresThe output has three columns: lines of IR, number of copies, and the function name. A line looks like Vec<T>::push with many copies. Many copies of the same generic function means the compiler instantiated it for many different types. This is monomorphization, covered in depth in a later chapter.
Some practical notes:
- By default the tool builds your package's library. For a binary, use
cargo llvm-lines --bin app, whereappis your binary's name. Add--releaseto measure the optimized IR, which is what matters for release builds. - The tool counts the crate it builds, including generic code instantiated from dependencies. A standard library function such as
Option::mapappearing with 200 copies is a hint, not a bug. - Large totals are not automatically a problem. Look for functions that are both large and have many copies, because those multiply.
Looking at dependencies with cargo tree
Slow dependencies are often slow because they were built with more features than you need, or because two versions of the same crate are built. cargo tree answers both questions.
# Show which features are enabled on each dependency
cargo tree -e features
# Show crates that appear in more than one version
cargo tree -d
# Show who depends on a crate (inverted tree)
cargo tree -i synUse -i (inverted) when you see a heavy crate in the timings report and want to know why it is there. The output walks from that crate back up to your own packages. If one small optional dependency pulls in a large tree, you have found something to trim.
Duplicate versions from cargo tree -d mean the same crate is compiled twice. This is common after a dependency bump that has not yet propagated everywhere. Sometimes cargo update resolves it. Sometimes you must wait for an upstream release.
Compiler self-reporting on nightly
If you use a nightly toolchain, rustc can report where its own time goes, including type checking, borrow checking, and monomorphization. Two options are common:
# Print time spent in each compiler pass for the final crate
cargo +nightly rustc --release -- -Z time-passes
# Write a self-profile that can be analyzed with the measureme tools
RUSTFLAGS="-Z self-profile" cargo +nightly buildThese options are unstable, and their output format can change. Treat them as a diagnostic tool for digging into one crate, not part of your regular workflow. A later chapter lists other nightly options.
A repeatable measurement habit
Wrap your measurements so you can repeat them exactly. The script below times a clean build and an incremental rebuild after touching one source file. Adjust the file path to your project.
#!/usr/bin/env bash
set -euo pipefail
FILE="src/main.rs"
echo "== clean build =="
cargo clean
time cargo build
echo "== no-op build =="
time cargo build
echo "== incremental rebuild after touching $FILE =="
touch "$FILE"
time cargo buildtouch updates the file's modification time without changing its content. Cargo then rebuilds the crate, but the compiler's incremental cache can reuse most of the work, so this measures a best case. A real edit that changes a public signature will cost more. When you try a change, rerun the same script and keep a note of the three numbers before and after.
Common mistakes
- Measuring several changes at once. If you switch linkers, change the profile, and trim features together, you cannot tell which one helped. Change one thing, then re-measure.
- Comparing a cold run to a warm run. A build that reuses
target/is not comparable to one aftercargo clean. - Optimizing the wrong profile. A fix measured in
devmay do nothing forrelease, and the reverse. Time the profile you actually wait on. - Trusting one number. Timings vary by a few percent from run to run. Ignore differences smaller than that.
- Looking only at total time. Two builds can take the same time with very different shapes. One may be bound by a serial chain, the other by a long link step.
Exercise
You run cargo build --timings on a clean build. The report shows 16 cores, but the concurrency chart stays at 1 or 2 active units for the last two minutes, and one long bar for your own crate covers that period. Would buying a machine with more cores help, and what two tools would you use next to find out why that crate is slow?
Show answer
More cores would not help much, because the concurrency chart shows the build is limited by a dependency chain. One crate is on the critical path, and the other units are idle or waiting on it. Next, run cargo llvm-lines (with --lib or --bin as appropriate) to see whether that crate generates a large amount of IR through generics, and run cargo tree -e features (and cargo tree -i on its dependencies) to check whether its dependencies carry unneeded features or heavy crates. Then change one thing and re-measure with the same scenario.
Exercise
A teammate says the project builds slowly. After touch src/main.rs and cargo build, the build takes 45 seconds, but the Codegen part of your binary's bar in the timings report is only 5 seconds. What do you check first, and why?
Show answer
Check the link step. Most of the 45 seconds is not code generation, so the time is going either to the front end of that crate or, more likely given a small codegen share, to the linker, which runs after rustc finishes. Run cargo build -v to see the linker command, and time the link on its own. If the link dominates, a faster linker is the next change to try, covered in a later chapter.