|
1 | 1 | +++ |
2 | 2 | title = "Tips For Faster Rust Compile Times" |
3 | 3 | date = 2024-01-12 |
4 | | -updated = 2025-09-11 |
| 4 | +updated = 2026-01-14 |
5 | 5 | draft = false |
6 | 6 | template = "article.html" |
7 | 7 | [extra] |
@@ -43,6 +43,7 @@ Click here to expand the table of contents. |
43 | 43 | - [Update Dependencies](#update-dependencies) |
44 | 44 | - [Find the slow crate in your codebase](#find-the-slow-crate-in-your-codebase) |
45 | 45 | - [Profile Compile Times](#profile-compile-times) |
| 46 | + - [Troubleshoot Slow Incremental Builds](#troubleshoot-slow-incremental-builds) |
46 | 47 | - [Replace Heavy Dependencies](#replace-heavy-dependencies) |
47 | 48 | - [Split Big Crates Into Smaller Ones Using Workspaces](#split-big-crates-into-smaller-ones-using-workspaces) |
48 | 49 | - [Disable Unused Features Of Crate Dependencies](#disable-unused-features-of-crate-dependencies) |
@@ -245,6 +246,28 @@ $ cargo llvm-lines | head -20 |
245 | 246 | 284 (0.9%) 4 (0.4%) core::option::Option<T>::ok_or_else |
246 | 247 | ``` |
247 | 248 |
|
| 249 | +### Troubleshoot Slow Incremental Builds |
| 250 | + |
| 251 | +If your incremental builds are slower than expected, you might be bottlenecked on a particular crate in the rustc backend. |
| 252 | + |
| 253 | +To diagnose this, you can use [`samply`](https://github.com/mstange/samply) combined with the `-Zhuman_readable_cgu_names=yes` flag to profile the compiler and identify which codegen units are taking the longest to compile: |
| 254 | + |
| 255 | +```bash |
| 256 | +samply record cargo build |
| 257 | +``` |
| 258 | + |
| 259 | +This will help you identify if a particular crate is the long pole in your build. Once you've identified the bottleneck, you may be able to improve compile times by refactoring or splitting the problematic crate. |
| 260 | + |
| 261 | +For more details on back-end parallelism in the Rust compiler, see [Nicholas Nethercote's blog post](https://nnethercote.github.io/2023/07/11/back-end-parallelism-in-the-rust-compiler.html). |
| 262 | + |
| 263 | +Another useful flag is `-Zprint-mono-items=yes`, which prints all [monomorphized items](https://rustc-dev-guide.rust-lang.org/backend/monomorph.html) during compilation. This can help you understand what's being generated in each CGU. |
| 264 | + |
| 265 | +```sh |
| 266 | +RUSTFLAGS="-Zprint-mono-items=yes" cargo +nightly build ⏎ |
| 267 | +``` |
| 268 | + |
| 269 | +_Thanks to [Caspar](https://slowrush.dev) (asparck on [Reddit](https://www.reddit.com/r/rust/comments/1q8x25s/debugging_a_slowcompiling_codegen_unit/)) for this tip!_ |
| 270 | + |
248 | 271 | ### Replace Heavy Dependencies |
249 | 272 |
|
250 | 273 | From time to time, it helps to shop around for more lightweight alternatives to |
|
0 commit comments