Jump to content

How One Step Made My Rust Build 10x Smoother

From JOHNWICK

My Rust builds were taking forever. Like, go-make-coffee-and-come-back forever. Eight minutes for a full build. Every small change meant waiting around, losing focus, checking Twitter. You know the drill.

Then I changed one thing. Build time dropped to 45 seconds. I’m gonna tell you what I did, but first, let me explain why Rust builds are slow in the first place.


Why Rust Builds Take So Long Rust does a lot of work at compile time. It’s checking ownership rules, validating lifetimes, monomorphizing generics, running optimizations. All that safety and performance? It doesn’t come free. When you hit cargo build --release, the compiler goes full tryhard mode. Every optimization, every inline decision, every piece of code gets scrutinized. That's great for your final binary, but terrible for development. Here’s what a typical build looks like: cargo build --release This compiles everything with full optimizations. Takes ages. Produces a fast binary. But during development? You don’t need that. The One Change That Fixed Everything I split my builds into two profiles. Development got fast, production stayed optimized. Here’s what I added to Cargo.toml: [profile.dev] opt-level = 1

[profile.dev.package."*"] opt-level = 2 That’s it. Two settings. What this does:

  • My code compiles with minimal optimization (opt-level = 1)
  • Dependencies compile with more optimization (opt-level = 2)

Why does this work? Because I change my code constantly, but dependencies rarely change. When I modify my code, only my code recompiles quickly. Dependencies stay cached with their optimizations. Before and After Before: $ time cargo build Finished dev [unoptimized] target(s) in 8m 12s After: $ time cargo build Finished dev [optimized] target(s) in 0m 47s That’s a 10x improvement. Not exaggerating. Other Things That Actually Helped Once I got the profile thing working, I tried a few more tweaks. 1. Use a Better Linker The default linker is slow. I switched to lld on Linux and zld on Mac. In .cargo/config.toml: [target.x86_64-unknown-linux-gnu] rustflags = ["-C", "link-arg=-fuse-ld=lld"]

[target.x86_64-apple-darwin] rustflags = ["-C", "link-arg=-fuse-ld=/usr/local/bin/zld"] Linking time dropped from 15 seconds to 3 seconds. Not huge, but noticeable. 2. Incremental Compilation This is enabled by default in dev builds, but I’ll mention it anyway because it’s important. [profile.dev] incremental = true Incremental compilation only recompiles what changed. If you turn this off (don’t), your builds will be miserable. 3. Parallel Compilation Cargo compiles in parallel by default, but you can tune it: cargo build -j 8 Or set it in .cargo/config.toml: [build] jobs = 8 I have 8 cores, so I use 8 jobs. Your mileage may vary. What Didn’t Work I tried sccache (a compiler cache). Setup was annoying, and it didn't help much because incremental compilation already handles most of the caching. I also tried reducing dependencies. Yeah, that helps, but it’s not practical when you need those dependencies. The Real Impact Before these changes, I’d make a small fix, hit build, and immediately context switch. Check Slack. Read an article. Lose my train of thought. Now? I make a change, hit build, and by the time I look back at the terminal, it’s done. I stay in flow. That’s the real win. Fast builds aren’t just about saving time. They’re about maintaining focus. When your feedback loop is 45 seconds instead of 8 minutes, you catch bugs faster, iterate quicker, and stay in the zone. Should You Do This? If you’re building a library or a small app, maybe you don’t care. Builds are already fast enough. But if you’re working on a larger project with dozens of dependencies, this is a no-brainer. Five minutes of config changes for 10x faster builds? Yeah, worth it. One More Thing Don’t use --release during development. Seriously. I see people do this all the time. They complain about slow builds while compiling with full optimizations enabled. cargo build # Fast, good enough for dev cargo build --release # Slow, for production only Save --release for when you're actually shipping. Your sanity will thank you.

Read the full article here: https://codingplainenglish.medium.com/how-one-step-made-my-rust-build-10x-smoother-5bbc55b69a84