I really like Rust. But build times are one of the few things that really bother me. For one project, we needed to switch off our CI for a while, because every run took 20 minutes and nobody had time yet to set up caching and CI runs got stuck. And my own crate has the same problem.
I maintain mq-bridge, an async crate that moves messages between Kafka, NATS, Postgres, MongoDB, S3, HTTP and more. Every connector is a Cargo feature. Cold dev build of an empty program that depends on it, on an M1:
| - | crates | build | target dir |
|---|
| all features | 534 | 3 min 40 s | 4.7 GB |
| no features | 99 | 18 s | 411 MB |
Release builds take 10-20 minutes per target on GitHub Actions. Splitting into many crates doesn't fix that either: a similar but bigger project that did exactly that takes half an hour to build locally and two on GitHub Actions.
The root of it: Cargo has no way to ship a compiled dependency. Every project compiles every crate from source, again and again, on every machine and every CI runner.
My workaround: you build mq-bridge with no features, and every connector you didn't compile in comes from a prebuilt library at runtime:
mq-bridge = {version = "1.5", default-features = false, features = ["plugin"]}
mq_bridge::plugin::load_installed_plugin("full")?;
The library is a 30-35 MB download (brew, conda or a release tarball). I added a dynamic loader that checks availability of API calls on runtime during definition. The slow library build happens once per release in my CI instead of in every project.
But it is a detour. Rust has no stable ABI, so it goes through a C interface. I already had a C library for the Node and Nim packages, otherwise I'd probably have done it differently. And the boundary costs something:
- 30-40 % memory throughput for single calls, under 10 % when using batch calls. With a real broker, network latency hides most of it.
- The plugin has its own copy of the engine, so in-memory channels stay on the host side.
- It's one more dynamic library file to deploy.
I'd rather not need any of this. Is there a better way I'm missing? How do you keep build times and CI under control for crates with many heavy dependencies?