Section 22 of 25

Build and test time

Contents

New in version 1.2.

The largest saving in SLAT comes from section 5: only app knows Tauri, so the tests of every other crate never compile Tauri and the web view behind it. The second largest comes from 5.3: a heavy native library lives in a driver crate that core does not depend on, so the tests of core never compile it either. The rest of the time goes to a few settings and habits, each a line or two of configuration. This section lists them in the order of what they usually save, and ends with the things that make a build start over when it should not.

Measure first. cargo build --timings writes an HTML report of every crate's compile time and of how many ran in parallel. Keep the report from before a change and compare.

22.1. Where the time goes

  • A library crate is compiled, not linked. What gets linked is every binary, and in a test run every test binary.
  • Each crate can have three kinds of test binaries: its unit tests, each file directly in tests/ (section 14.1 reduces that to one, tests/it), and its documentation examples. Since edition 2024, the documentation examples of a crate are built as one binary. A crate with no documentation examples sets doctest = false under [lib].
  • A test run below app never needs Tauri. The everyday command is cargo t (22.3), which runs every crate except quayside. Only the end-to-end tests of section 14 build the app crate.
  • A heavy native library costs more than Tauri. C and C++ sources compiled by build scripts, and machine-learning crates, take most of a clean build and most of the link of every binary that includes them. Section 5.3 keeps them out of the tests of core.
  • Debug information is a large share of the link time. Most of it is never read.

22.2. Build settings

In the workspace root Cargo.toml:

[profile.dev]
debug = "line-tables-only"
split-debuginfo = "unpacked"

[profile.dev.package."*"]
debug = false
strip = false

Your own crates keep file and line numbers in backtraces; dependencies get no debug information. Raise it back to debug = true for one session when you need a debugger.

strip = false is not optional on macOS. With debug = false, Cargo also strips those units by default, and on macOS 27 the system strip damages the dynamic libraries of procedural macros: serde_derive can no longer be loaded and a clean build fails. split-debuginfo = "unpacked" is written out so that whether dsymutil runs after every link on macOS does not depend on the default of a given Cargo version.

The linker matters most for test binaries. On Linux for x86-64, Rust has used rust-lld by default since version 1.90. Other Linux targets can use mold. On macOS, the linker that comes with Xcode 15 and later is already fast, so leave it alone. On Windows, rust-lld is usually much faster than the default linker. In .cargo/config.toml:

[target.aarch64-unknown-linux-gnu]
linker = "clang"
rustflags = ["-C", "link-arg=-fuse-ld=mold"]

[target.x86_64-pc-windows-msvc]
linker = "rust-lld.exe"

Optimize the dependencies your tests spend their time in. A dev build compiles every dependency without optimization, including an encrypted database built from C sources, a cipher or a hash function that the tests call millions of times. Optimizing only those packages makes the tests faster and leaves the release build unchanged. In one of our workspaces this took a test run from 170 to 102 seconds:

[profile.dev.package.libsqlite3-sys]
opt-level = 3

[profile.dev.package.sha2]
opt-level = 3

Each line costs one rebuild of that package. Measure the test run before and after, and keep only the packages that moved it.

Remove dependencies nobody uses: cargo machete lists them, and each one is compile time for nothing.

22.3. One set of features

Cargo builds a dependency once for every distinct set of features it is asked for. Two things in a SLAT workspace ask for different sets:

  1. Different commands. cargo test --workspace unifies the features of all members, cargo test -p quayside-core only those that core needs. Running both compiles tokio, serde and the rest twice. cargo-hakari fixes this: it keeps a generated workspace-hack crate that every member depends on, with the union of all features, so every command builds the dependencies the same way. cargo hakari generate updates it, and cargo hakari verify in CI fails when someone forgot.
  2. Test features of your own crates. The fake feature of 14.2 and test-hooks of section 10 change drivers and core, so every crate above them is compiled again when the feature switches. Prefer a data seam (14.2), which needs no feature. Where a feature is necessary, every test command switches it on the same way. Put that command in one place, in .cargo/config.toml:
[alias]
t = "nextest run --workspace --exclude quayside --features quayside-drivers/fake --no-tests=fail"

Everyone, including agents and CI, runs cargo t for the whole suite, and every narrower test command is cargo t with a filter added (22.4). A test command with a different list of packages or features is a second build. A workspace that uses only data seams (14.2) drops --features quayside-drivers/fake here and in the script of 22.4: an alias that names a feature nobody defines fails.

22.4. Run what a change can affect

The fixed layout says which slice a file belongs to, and the slice graph from the cycle check (16.4) says which slices depend on it. Together they give each kind of change its own set of tests:

What changed What runs
logic.rs, model.rs, store.rs, service.rs or actor/ inside one slice, its mod.rs unchanged the tests of that slice, and cargo mutants --in-diff (14.3)
a slice's mod.rs, its public API that slice and every slice that imports it
a module of tests/it that module
kit, store or drivers every crate that depends on it: cargo t -E 'rdeps(quayside-store)'
drivers/src/platform/ also the contract test (12.5); in CI, the platform job (16.3)
only app the end-to-end tests through the entry; nothing in core changed
Cargo.toml, Cargo.lock, .cargo/, the toolchain file everything

A filter chooses which tests run, not what is compiled: nextest first builds every package that the Cargo options name, then applies -E. That is why every row keeps the packages and features of cargo t and narrows only with -E. The build stays the one that is already in target/, and only the run gets shorter. Narrowing the build with -p saves compile time only when cargo-hakari (22.3) keeps the features the same; without it, -p is a second build of the dependencies.

The tests of a slice are easy to select because their names start with the slice: unit tests live in core/src/<slice>/, so their names begin with <slice>::, and the modules of tests/it begin with the slice too (recording_stops_on_time_limit). A first version of the selection fits in a short script. It compares the working copy with the point where the branch left main, so changes that are not committed yet count too, which is what an agent's loop needs:

#!/usr/bin/env bash
set -euo pipefail

base="$(git merge-base origin/main HEAD)"
changed="$({ git diff --name-only "$base"; git ls-files --others --exclude-standard; } | sort -u)"
crate="crates/quayside-core"
tests=(cargo nextest run --workspace --exclude quayside --features quayside-drivers/fake --no-tests=fail)

if grep -qE '(^|/)Cargo\.(toml|lock)$|^\.cargo/|^rust-toolchain|^crates/quayside-(kit|store|drivers)/' <<< "$changed"; then
  exec "${tests[@]}"
fi
if grep -qE "^$crate/(src/[^/]+\.rs|src/.*mod\.rs|tests/it/main\.rs|tests/it/support/)" <<< "$changed"; then
  exec "${tests[@]}" -E 'package(quayside-core)'
fi

names="$({
  sed -nE "s#^$crate/src/([a-z_]+)/.*#\1#p" <<< "$changed"
  sed -nE "s#^$crate/tests/it/([a-z_]+)\.rs\$#\1#p" <<< "$changed"
} | sort -u | paste -sd'|' -)"
if [ -z "$names" ]; then
  echo "nothing below app changed; run the end-to-end tests of the entry"
  exit 0
fi
exec "${tests[@]}" -E "package(quayside-core) & test(/^($names)(::|_)/)"

It is deliberately cautious: a change to any mod.rs, to a file at the top of core/src/ or to the shared test helpers runs all of core instead of following the slice graph. Replace that step with the graph when the suite of core gets slow.

Selection is for speed, not for proof. Use three levels:

Where What runs
on your machine and in an agent's loop the selection above
in CI on a pull request every crate that depends on a changed crate (rdeps)
on main, or in the merge queue the whole suite

--no-tests=fail (14.1) stays on at every level, so a selection that matches nothing is red, not quietly green.

22.5. Many working copies

Coding agents often work in parallel, each in its own working copy. Without care, four agents compile the same dependencies four times at the same moment.

Every working copy has its own target/. Do not point several copies at one folder, with a symbolic link or with CARGO_TARGET_DIR. It looks like a shared cache, but it is a correctness bug. Two copies of the same workspace produce the same artifact names and the same fingerprint entries, so after build A; build B; build A Cargo can report copy A as Fresh while the library on disk was built from the sources of B. A check then judges someone else's code and reports green. On top of that, branches that differ rebuild each other's crates at every switch.

Start a new copy warm by cloning, not by sharing. When the working copy is created, copy the target/ of the main copy with a copy-on-write clone: cp -c -R on macOS (APFS), cp --reflink=auto -R on Btrfs or XFS. It takes seconds and no disk space until a file changes. Dependencies stay fresh, and Cargo rebuilds only your own crates, because their sources are newer than what the clone recorded. Measure the first build in a new copy once; it should be close to an incremental build, not a clean one.

sccache is a complement, not the answer. Set it as the compiler wrapper, in the user's ~/.cargo/config.toml or in the environment (RUSTC_WRAPPER=sccache), and its local cache is shared by every working copy on the machine. It caches the compilation of ordinary library dependencies. It does not cache procedural macros, binaries, cdylib crates or your own crates, which Cargo compiles incrementally, and it does not cache linking. Linking the test binaries is what each working copy pays most for, and only 14.1, 22.2 and the clone above reduce it.

Limit how much of the machine one build may take with CARGO_BUILD_JOBS, for example half of the cores, so that a second build slows down instead of freezing the machine. The rule of section 19.2 still holds: one heavy build at a time.

22.6. CI

  • A cache in every Rust job, including the platform job of 16.3. Swatinem/rust-cache keeps dependencies and the target folder between runs, keyed by Cargo.lock and the toolchain.
  • Write the cache only from main, read it everywhere. A pull-request run can read the cache of its own ref and of the default branch, nothing else, so a cache written by one pull request helps only that pull request, while the default branch, where every other pull request looks, stays empty. Set save-if: ${{ github.ref == 'refs/heads/main' }} and run the workflow on push to main as well, so that every merge refreshes the cache and the first run of each pull request starts warm. In one of our repositories that took the first run from 942 to 478 seconds.
  • CARGO_INCREMENTAL=0 in CI. Incremental data is large and useless in a fresh run. Swatinem/rust-cache sets it for you; with any other cache, set it yourself.
  • The tests below app on Linux. When the only macOS code is in drivers/src/platform/ and in the native driver crates of 5.3, the tests of kit, store and core need no macOS. Run them on a Linux runner, which starts faster and costs less, and keep the macOS runner for drivers, app and the contract test.
  • Build once, test in parallel, when the suite takes longer than a few minutes: cargo nextest archive builds the test binaries in one job, and several jobs run their part of them with --partition hash:1/3, 2/3, 3/3.
  • A timings report on main. cargo build --timings as an artifact of every build of main. When the bar of core keeps getting longer after a change to a single slice, that is the measurement section 5.3 asks for before a split.
  • cargo hakari verify and cargo deny check next to the checks of section 16. Both take seconds.

22.7. What makes a build start over

A build that starts over for no visible reason almost always has one of these causes:

  • A build script without rerun-if-changed. A build.rs that prints no cargo:rerun-if-changed runs again after a change to any file of its package, and everything that uses its output is compiled again. Print one line per input. A script that compiles helper programs (Swift, C) lists their sources. Never list a path that does not exist: Cargo treats it as changed on every build.
  • tauri dev watching its own output. The development server rebuilds the app when a file in src-tauri/ changes. A database, a log or a generated file that the app writes there restarts the build in a loop. Runtime data belongs under kit::paths (12.4); generated files go into .taurignore.
  • The editor and the terminal in one folder. rust-analyzer runs cargo check with its own settings. In the same target/, the editor and the terminal or an agent wait for each other's lock and can invalidate each other's work. Give the editor its own folder: "rust-analyzer.cargo.targetDir": true.
  • Different environments. RUSTFLAGS, and the variables that build scripts read (MACOSX_DEPLOYMENT_TARGET, CC, CFLAGS), are part of what Cargo compares. If the terminal, the editor and an agent's shell set them differently, every switch between them rebuilds. Put them in .cargo/config.toml ([env], [build] rustflags), not in shell profiles.
  • A moving toolchain. A new compiler rebuilds everything. Pin the full version in rust-toolchain.toml, three parts (1.98.1): a two-part channel picks up every new patch release without a change in the repository.

22.8. Clean target/

Cargo never deletes old artifacts. Every branch, feature set and dependency version leaves files behind, and with hundreds of thousands of files in target/ every Cargo command gets slower. In one of our workspaces cargo clean removed about 960,000 files (119 GiB). A full build of the library and its tests went from 7–8 minutes to 36 seconds, and cargo clippy --all-targets from several minutes to 8 seconds.

Make it a rule, not a rescue: when target/debug/deps passes a few tens of thousands of files, or target/ passes a few tens of gigabytes, run cargo clean and accept one cold build. cargo sweep --time 30 is the gentler option: it removes only the artifacts not used for 30 days. A working copy that is removed takes its target/ with it.