#Rust parallel compiler
72 messages · Page 1 of 1 (latest)
If it helps compile time, sure, although I do build sometimes on a 2core server, which I don't want getting much slower
I mean, duh, people want things to be faster 
I've personally not had a problem with compile times on my dev machine (lots of cores). I have a bigger problem with them on CI with single threaded VMs 
the Rust compiler is already parallelized to a good degree, but the fact of the matter is that the compiler still spends most of the time in codegen
since individual crates undergo codegen separately (and the final bin, staticlib, cdylib or dylib crate drives some of the codegen for things like generic code and #[inline]), it's naturally parallel to some extent
if you have at least 12 dependencies on a 12-thread processor, the compilation process will just be parallel and concurrent without any sort of additional synchronization overhead: the OS scheduler and I/O manager will do the heavy lifting in a lightweight fashion, and Cargo will orchestrate the concurrent build process
in general, with individual crates being compilation units (same build process role as individual .c/.cpp files in C/C++), splitting up your code into multiple crates has a positive effect on compile times
Not really, actually
Currently rustc isn't really parallel at all, the only "parallelism" comes from cargo spawning multiple rustc instances, each crate is entirely serial in its processing
And codegen is the one of the few things that actually is parallelized right now
Monomorphisation could probably benefit from parallelism. I remember that being kinda slow on some projects
that's exactly the point I'm making: rustc itself won't benefit nearly as much from its own parallelism as people would expect it to
it would only marginally gain performance when the amount of remaining crates to compile drops below the amount of threads in the system
No, parallelism allows rust to be parallel over a single crate
Meaning that each individual crate's compilation is faster
in theory, yes
in practice, if 12 crates are compiling in parallel on a machine with 12 threads, that (ideally) puts it at 100% CPU usage
it doesn't help if you create more threads in every process
they'd just be competing for CPU time with each other
Depends on the dependency pipeline. If cargo could know the pipeline and how many crates it expects will be at each point, then it can tell rustc to use a different num of threads
very slow in things like warp
Yup...
last time I used hardware of that sort, I couldn't really get Rust up and running because the browser froze for several hours way before I could navigate to the Rustup installer download page
webtech is horrid
if rust could compile huge blocker crates like syn or proc_macro2 in parallel it would probably make a noticably difference
it could, but only if those kick in fairly lately in the build process
Cargo could start compiling large crates first based on some heuristic or even manual configuration, which may reduce the chance that they're gonna end up last in the build queue with no other jobs to work on the other cores at the same time
Parallel rustc would allow running procs in parallel, if that's what you're meaning
s/would/could/
let me compile a project or mine really quick with --timings
other than that, there's a fundamental limitation to how much you can improve performance by implementing concurrency in individual tasks
Yah, but luckily lots of the things that rustc does are super easy to parallelize
Lotta for loops and things that are generally unrelated to each other
if syn was 3 times as fast, I would have saved probably more than 10% of the entire compile time
also fully blocking crates in at the end, would be nice if those were faster
and if tokio wasn't so slow, hyper could have started a lot earlier
this makes sense, I never thought about how much time Cargo spends blocking on other crates
this shows really well how cargo parallelizes a lot which makes things bearable, but we're far from always being maxed out, many bottlenecks
Parallel compilation essentially aims to lower the per-crate compile time
Which ideally increases overall throughput
syn's actually one of the best examples you could get, you can't just reorganize the build to extract initial metadata from the crate, supply it to concurrent build jobs and let codegen run as a separate story
so lots of crates just sit around waiting for another rustc instance to explain them what a Punctuated is
Yep, pretty much
you have this chain
proc-macro2 build script -> proc-macro2(build) -> proc-macro2 -> quote -> syn -> tokio-macros -> tokio -> hyper -> axum -> my crate
no matter how many cores you have, this will be the bottleneck
and fun fact, since these were almost chained, it wouldn't have made a big difference whether I had just those crates or 100 others, this was my compile time
Yah, on a hyper-ideal level rustc would actually know what crates are, but I don't know how conceivable that is
That'd allow running everything within one process where all resources would be shared as efficiently as possible
Only the things actually needed for the final binary would be further processed
I would have saved over 2 seconds if proc-macro2 didn't need a build script :(
this is a fun TIL, I knew that there were blocking crates and all, but I wasn't aware that my compile times could not have been really faster even with twice as many cores
until rustc gets more parallel that is 
Yah, compilation is ultimately (right now) limited by the dependency tree
That's why a lot of times you'll end up sitting on the very last crate (the current one) being compiled while nothing else happens
the dependency tree, also known as proc-macro2 -> quote -> syn
yes, at the end everything slows down since it slowly becomes one
there are two highly parallel batches, pre-syn, where many small crates that don't transitively depend on syn get compiled, then everything slows down and waits for syn, after which the post-syn batch gets started where everything speeds up again
syn is definitely the most influential crate for build times
It's not really fair to blame syn
I'm not "blaming" syn
Any crate that lots of things depend on will have this effect
well, I'm blaming syn but not in a negative way
It's the leftpad effect, kinda
syn is a very good crate, and the rust ecosystem would be worse without it, but it's a very interesting case study about scheduling and build times