r/rust • u/TheAnalist_ • 1d ago
🙋 seeking help & advice Faster compilation tricks
I’m developing epicenter using only features Cargo already provides to cut compile times and disk usage. The main one is a shared build-dir plus a store of already-compiled dependencies, so rustc doesn’t rebuild the same deps for every new project. On top of that I switched the linker to rust-lld and enabled incremental compilation for all profiles.
These three together already cut build times and disk usage significantly, but my goal is to push both as low as possible, so I’m willing to accept trade-offs (e.g. less debuginfo, slightly slower runtime in dev builds) as long as the defaults stay safe for most users. Is there anything else I’m missing? I’m thinking of things like split-debuginfo, reduced debug levels, per-package opt-level overrides for dependencies, or other Cargo/rustc settings that are not enabled by default but would pay off in this setup.
1
u/eikenberry 21h ago
Is there a resource online that gives instructions on how to do these optimizations to reduce compile times for someone new to the language?
2
u/TheAnalist_ 21h ago
mostly the rust documentation, https://doc.rust-lang.org/stable/cargo/guide/build-performance.html
but I find it works best to test things out
1
u/epage 12h ago
Our guide is fairly conservative (e.g. no share. build-dir).
For more, see https://corrode.dev/blog/tips-for-faster-rust-compile-times/
3
u/rotundjose8 1d ago
split-debuginfo is a big one for disk usage, especially if you're building with debuginfo still on. offloading the dwarf to a separate file means the linker doesn't have to juggle it all in memory and your main binary stays lean.
for compile time, cranking the codegen units up in the dev profile can trade a bit of runtime perf for faster builds. also check if you can bump the lto setting down to "off" for dev if it's not already.
2
u/BusinessBandicoot 22h ago
For debug use line tables only, thin lto, and wild. You can create a separate profile for actually attaching a debugger
2
u/Upstairs-Project2756 7h ago
try setting debug to line-tables-only and opt-level 1 or 2 for dependencies. This drops compile time significantly without needing incremental builds or lld since the heavy lifting happens on dep code that changes rarely
0
u/arjuna93 22h ago
The best trick is to use a different language
1
u/rodrigocfd 1h ago
People will downvote you, but you have a point.
For a project, the choice of a programming language involves pros and cons, and one must consider all them. Rust will give you top notch correctness/performance, at the expense of long compilation times.
At the other side of the spectrum, we have languages like Go, which was specifically designed for fast compile times, at the expense of less correctness/performance. If compilation time is so important to OP, maybe a proof-of-concept should be considered.
Programming languages are just tools.
2
u/dgkimpton 23h ago
Are you talking release mode or debug mode? In Debug I'm pretty sure most (if not all) optimisations are already off. Splitting debug symbols is just extra work.
Your best wins will splitting your project into smaller crates, more RAM, faster SSD, faster CPU, possibly a faster linker (wild?).
I'm not sure, but I suspect
cargo build --frozenwill yield marginal gains due to not checking for newer versions online.