ADR 19: Rust Build Optimization
Status: Accepted Date: 2026-04-26
Context
As the Substratum project grows, compiling the Rust workspace (crates/* and apps/*) becomes increasingly slow, particularly during local Docker development. The default Linux linker used in our Debian containers is inefficient, and compiling third-party dependencies takes unnecessary time when Docker containers are spun up from scratch.
We need a stable, performant build pipeline that leverages fast Rust compilation techniques. While we use Nx for the monorepo orchestration, we are currently deferring full native Nx Rust integration (e.g., custom project.json targets) until our CI/CD pipeline is built. For now, our primary focus is optimizing the local developer experience exclusively inside Docker containers running Debian.
Decision
We are implementing a multi-tiered build optimization strategy composed of Cargo configuration tuning:
Linker and Caching (
.cargo/config.toml& Docker):- For Docker containers (e.g., our
debian:bookworm-slimimages), the build runs in a Linux environment. We explicitly configure Cargo to use the fast LLVM linker (lld) for Linux target triples (x86_64-unknown-linux-gnuandaarch64-unknown-linux-gnu) to significantly speed up the linking phase of compilation. - We require
sccacheas therustc-wrapperto provide persistent, cross-branch caching of compiled Rust artifacts. - For Docker (
Dockerfile.gateway),sccache,lld, andclangare installed in the build container, and the cache is persisted to the host via named volumes (sccache_datamapped to/var/cache/sccache,gateway_targetmapped to/app/target, andcargo_registrymapped to/usr/local/cargo/registry).
- For Docker containers (e.g., our
Dependency Optimization (
Cargo.toml):- We removed previous experimental overrides for
opt-level = 3on third-party dependencies. Whileopt-level = 3provides significantly faster runtime execution for cryptographic operations and networking during local testing, it caused initial Docker volume population to take upwards of 20 minutes. We favor fast developer onboarding and iteration (opt-level = 0) combined with persistent Docker volumes.
- We removed previous experimental overrides for
Consequences
- Positive:
- Massive reduction in incremental build times due to
lldandsccache. Subsequent Docker rebuilds are nearly instantaneous. - Complete stability; no crashing daemons caused by third-party plugins.
- Initial Docker builds are extremely fast (1-2 minutes) by keeping
opt-level = 0and persisting volumes.
- Massive reduction in incremental build times due to
- Negative:
- Local execution of the Gateway and Sidecar will be noticeably slower (especially cryptographic hashing) compared to production due to the lack of dependency optimizations in the dev profile.