8 Solutions for Troubleshooting Your Rust Build Times

  1. Take all crates and generate a DAG which is a convenient way to understand which crate depends on (or blocks the compilation of) another crate, and by that, for example — Rust will be able to understand which crates can run in parallel and use all machine cores
  2. If that’s not enough, divide each crate into codegen units to let LLVM process them in parallel as well
  3. And lastly, incremental compilation helps because Rust will save part of the compilation planning work from a previous build and reuse it in the next
logo of llvm

1. Create a Speedy Build Experience

The first step to create a fast build is not to build at all. Using rust-analyzer in your editor and development process gives you just this experience. This means that you’re implicitly running cargo check which is the first tip for anyone trying to optimize their build speeds. Most people want fast feedback, and you can get that feedback with cargo check instead of cargo build.

2. Throw Hardware At It

picture of a broken bridge

3. Big Things Build Slowly

$ cargo install cargo-bloat
$ cargo bloat --release -n 10
$ cargo bloat --release --crates

4. Take a Deeper Look

In the case where we’re not so lucky, and unicorns don’t exist (sorry, unicorn fans!), we’re left with doing real build process analysis. We have a couple of ways to do that.

$ RUSTFLAGS="-Z time-passes" cargo build

A magical view for your build

Actually, the best trade-off between high detail and high productivity, is a built in cargo flag, called timings. It's such a great tool, it's almost magical and in fact, I use it exclusively, even for smaller troubleshooting session.

$ cargo build -Z timings

5. Replace Your Linker

While I don’t want to be too nostalgic (I’m tempted to), linking used to be a process you do by hand, or at least a process you’re very aware of, and used to be a common ceremony if you were doing assembler or C more than 20 years ago. Today it’s mostly transparent, or at least the experience of linking is.

What’s in a Faster Linker?

Why is replacing a linker faster? to get an impression, here’s the notes from zld's README :

  • Using Swiss Tables instead of STL for hash maps and sets
  • Parallelizing in various places (the parsing of libraries, writing the output file, sorting, etc.)
  • Optimizations around the hashing of strings (caching the hashes, using a better hash function, etc.)

Linux

In Linux the work is pretty much cut out for you by installing and using lld which is a much faster linker for Rust to use:

$ sudo apt-get install lld
[target.x86_64-unknown-linux-gnu]
linker = "/usr/bin/clang"
rustflags = ["-Clink-arg=-fuse-ld=lld", "-Zshare-generics=y"]
$ cargo build

macOS

This solution didn’t exist for a long while (you couldn’t do anything about linking speeds) for macOS, but recently we’ve got zld support and we can use it instead:

$ brew install michaeleisel/zld/zld
[target.x86_64-apple-darwin]
rustflags = ["-C", "link-arg=-fuse-ld=/usr/local/bin/zld", "-Zshare-generics=y"]
$ cargo build

Global Or Local Config?

I recommend setting your ~/.cargo/config globally to enjoy faster build time around your workstation, however if you're uneasy about replacing a linker for what ever reason you can always have it local in every project's .cargo folder.

6. Crate Shattering

crate shattered

7. Conditionally Compile a “Go Fast” Feature

Here’s a trick I’ve developed on my own, and I have indications in the Rust documentation for it but haven’t really seen it in the wild.

8. Use Small New Projects on the Side

When all fails, what works best is to just iterate quickly on an idea, a POC or a crate in a fresh folder, with this great feeling of starting from scratch: naturally a small amount of dependencies, small amount of source files, and low complexity for the compiler as a result.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Dotan Nahum

Dotan Nahum

2.9K Followers

@jondot | Founder & CEO @ Spectral. Rust + FP + Hacking + Cracking + OSS. Previously CTO @ HiredScore, Como, Conduit.