1

I have a Rust project of ~5k lines and about 15 dependencies. Compiling this takes pretty long, so I used cargo -Z timings to see what was causing the bottlenecks. Basically, packages related to jsonrpc seem to increase the compile time a lot, but I'm only using jsonrpc in one function so I don't see why my entire project needs to wait for jsonrpc to compile.

Is there a way to reorganize my project so that the rest of the project can compile in parallel with jsonrpc and then the final jsonrpc part can compile at the end?

Right now, my project is organized like this:

Cargo.toml
src/bin/binary_a.rs
src/bin/binary_b.rs
src/lib.rs
src/jsonrpc_using_mod.rs
src/mod_a.rs
src/mod_b.rs
...
src/mod_z.rs
Emre
  • 503
  • 4
  • 7
  • "The rest of the project can compile in parallel" How long does your 5k lines take to parallel alone? if your project is small enough, pipelining parts of it ahead of jsonrpc completion might not have significant advantage. – SOFe May 07 '20 at 15:22
  • 10% of the overall compilation is spent compiling my code (other than binaries). 10% is spent on compiling jsonrpc+dependencies that only belong to jsonrpc. The other major bottleneck is serde-derive but I don't think I can do anything about that. – Emre May 07 '20 at 15:28
  • 10%? How many seconds is that? If I remember correctly, 1/8 of serde-derive compilation time is not that significant. – SOFe May 07 '20 at 15:29
  • Furthermore, note that the ultimate obstacle to systems programming comes from linking time. Binary crates often take longer (especially compared to `cargo check`) to compile because of the required to link the intermediate objects into an executable. – SOFe May 07 '20 at 15:30
  • Supposing your question is valid, the answer to your question is quite trivial. Topo-sort your module dependencies. Split those above `jsonrpc_using_mod` to an inner package that gets included as a dependency of `jsonrpc_using_mod` along with other downstream modules that create the final executabe. – SOFe May 07 '20 at 15:33
  • yes, exactly that's what I was asking. How can I have an "inner package"? A separate Cargo.toml that needs to be compiled with a separate `cargo build`? – Emre May 07 '20 at 15:35
  • It is quite common to do this, especially for crates that need crate-specific proc macros. Just `cargo new` a new crate, then in the Cargo.toml of your original crate, add a `{path = "path/to/new/crate"}` entry to `[dependencies]`. – SOFe May 07 '20 at 15:39

0 Answers0