Skip to content

Commit 67b8355

Browse files
authored
perf(rust): share cargo intermediates across checkouts (#2446)
* perf(rust): share cargo intermediates across checkouts Every checkout compiles its own copy of the dependency graph. Anyone keeping more than one clone or worktree open pays that in full each time, around 1.6G apiece. build-dir moves only the intermediate artifacts out of the checkout, and it supports path templating, so {cargo-cache-home} resolves to CARGO_HOME and one shared location covers every checkout on a machine. Nothing absolute or machine specific is committed. target-dir was the obvious alternative and does not work here: it has no templating, cargo expands neither ~ nor $HOME, so a committed value could only be relative to the checkout. That would limit sharing to sibling directories, and because it also moves the final artifacts it would break the three places the BrowserClaw release locates a built binary. Final artifacts still land in <checkout>/target, so nothing that resolves a build output by path changes. Measured across two checkouts of the same branch: cold build 52.36s target 227M shared 1.6G second checkout 16.14s target 227M shared 2.1G A release build against a warm shared directory still produces target/release/browseros-claw-server-rs. rust-cache saves only workspace target dirs plus the registry and git caches, and never reads a build dir setting, so the shared directory is named to it explicitly. Without that, CI would recompile the dependency graph on every run. * ci(rust): warm the rust cache on main and drop it fortnightly Three related gaps around the shared cargo build directory. The Rust cache was never warm for a new pull request. Tests run only on pull_request, so rust-cache saved under a PR branch's scope, and branches cannot read each other's caches. This is the same problem the Turbo warm run already solves, and Rust was simply never covered. It matters more now that the intermediates live in a cache-directories entry: without a warm run, every PR recompiles the dependency graph. Warming alone would not have worked. rust-cache builds its key from GITHUB_JOB unless shared-key is set, and the existing keys show it: v0-rust-test-Linux-x64-<hash>-<hash> A warm job under any other name would have written a cache nothing else could read. Both steps now pin the same shared-key, workspaces, cache-directories and toolchain, since the toolchain hashes into the key too. The new warm job mirrors what the Rust suites compile, test binaries and clippy's separate artifacts, and deliberately omits -D warnings because it exists to populate a cache rather than to gate on lints. Finally, rust-cache prunes only workspace target dirs and never extra cache-directories, so the shared build directory is cached wholesale and grows without bound. It is already the larger part of the problem: v0-rust 25 entries 6.97 GB all caches 262 entries 10.35 GB against a 10 GB allowance Being over the allowance means LRU eviction is already discarding other caches. Dropping the Rust entries on the 1st and 15th keeps that bounded, matched on the prefix so nothing else is touched, and the warm workflow is dispatched straight after so no branch waits for the next merge.
1 parent 068f4c6 commit 67b8355

4 files changed

Lines changed: 129 additions & 4 deletions

File tree

.cargo/config.toml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[build]
2+
# Keep intermediate artifacts outside the checkout so every clone and worktree
3+
# on a machine shares one copy instead of each rebuilding the dependency graph.
4+
# {cargo-cache-home} expands to CARGO_HOME, so this is portable: no absolute
5+
# path, no assumption about where the repo lives. Cargo does not expand ~ or
6+
# $HOME, and target-dir does not support templating, so build-dir is the only
7+
# form that gets this.
8+
#
9+
# Final artifacts still land in <checkout>/target, so anything locating a built
10+
# binary by path keeps working.
11+
build-dir = "{cargo-cache-home}/build/browseros"
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
name: Rust Cache Bust
2+
3+
# The shared cargo build directory is cached as an extra `cache-directories`
4+
# entry, and rust-cache prunes only workspace target dirs, never extra ones. So
5+
# that entry is cached wholesale and accumulates artifacts for dependencies and
6+
# profiles that no longer exist. Left alone it grows against the repository's
7+
# 10GB Actions cache allowance and evicts entries that are still wanted.
8+
#
9+
# Dropping it on a schedule keeps that bounded. The next warm run rebuilds it
10+
# from scratch, so the only cost is one cold Rust compile.
11+
on:
12+
schedule:
13+
# 1st and 15th, so roughly fortnightly. Ahead of the Monday 06:00 warm run
14+
# rather than alongside it, and the warm workflow is dispatched below in any
15+
# case so a fresh cache does not wait for the next merge.
16+
- cron: "0 5 1,15 * *"
17+
workflow_dispatch:
18+
19+
permissions:
20+
actions: write
21+
22+
concurrency:
23+
group: rust-cache-bust
24+
cancel-in-progress: false
25+
26+
jobs:
27+
bust:
28+
name: Drop Rust caches
29+
runs-on: ubuntu-latest
30+
timeout-minutes: 10
31+
steps:
32+
- name: Delete Rust caches
33+
env:
34+
GH_TOKEN: ${{ github.token }}
35+
REPO: ${{ github.repository }}
36+
run: |
37+
set -euo pipefail
38+
# rust-cache prefixes every key it writes with `prefix-key`, which is
39+
# left at its default. Matching on that keeps Turbo and any other
40+
# cache in this repository untouched.
41+
ids="$(gh api --paginate "/repos/$REPO/actions/caches?per_page=100" \
42+
--jq '.actions_caches[] | select(.key | startswith("v0-rust")) | .id')"
43+
44+
if [ -z "$ids" ]; then
45+
echo "No Rust caches to drop."
46+
echo "No Rust caches to drop." >> "$GITHUB_STEP_SUMMARY"
47+
exit 0
48+
fi
49+
50+
count=0
51+
while read -r id; do
52+
[ -n "$id" ] || continue
53+
gh api -X DELETE "/repos/$REPO/actions/caches/$id"
54+
count=$((count + 1))
55+
done <<< "$ids"
56+
57+
echo "Dropped $count Rust cache entries."
58+
echo "Dropped $count Rust cache entries." >> "$GITHUB_STEP_SUMMARY"
59+
60+
- name: Request a fresh warm run
61+
env:
62+
GH_TOKEN: ${{ github.token }}
63+
REPO: ${{ github.repository }}
64+
run: |
65+
set -euo pipefail
66+
# Rebuild immediately rather than leaving every branch cold until the
67+
# next merge or the Monday schedule.
68+
gh workflow run turbo-cache-warm.yml --repo "$REPO" --ref main
69+
echo "Dispatched Turbo Cache Warm on main." >> "$GITHUB_STEP_SUMMARY"

.github/workflows/test.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,14 @@ jobs:
100100
uses: Swatinem/rust-cache@v2
101101
with:
102102
workspaces: packages/browseros-agent
103+
# Without this the key embeds GITHUB_JOB, so the warm cache produced
104+
# on main by a differently named job could never be restored here.
105+
shared-key: browseros-agent
106+
# Intermediate artifacts live outside the checkout so worktrees can
107+
# share them, and rust-cache only saves workspace target dirs plus the
108+
# registry and git caches by default. Name the build dir explicitly or
109+
# every run would recompile the dependency graph from scratch.
110+
cache-directories: ~/.cargo/build/browseros
103111

104112
- name: Install dependencies
105113
run: bun ci

.github/workflows/turbo-cache-warm.yml

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
name: Turbo Cache Warm
22

3-
# Populates the Turbo cache on `main` so PR branches inherit warm cache.
4-
# GitHub Actions caches restore only from the same branch or the base/default
5-
# branch, so without a run on `main` every PR would start cold. Neither the
6-
# Tests nor Code Quality workflows run on `main`, so this is that run.
3+
# Populates the Turbo and Rust caches on `main` so PR branches inherit warm
4+
# cache. GitHub Actions caches restore only from the same branch or the
5+
# base/default branch, so without a run on `main` every PR would start cold.
6+
# Neither the Tests nor Code Quality workflows run on `main`, so this is that
7+
# run.
78
on:
89
push:
910
branches: [main]
1011
paths:
1112
- .github/workflows/turbo-cache-warm.yml
13+
- .cargo/config.toml
1214
- packages/browseros-agent/**
1315
schedule:
1416
# Weekly full warm to backstop GitHub's 7-day unused-cache eviction for
@@ -63,3 +65,38 @@ jobs:
6365
# Per-merge: warm only what this merge changed.
6466
bunx turbo run typecheck --affected
6567
fi
68+
69+
warm-rust:
70+
name: Warm Rust cache
71+
runs-on: ubuntu-latest
72+
timeout-minutes: 45
73+
steps:
74+
- name: Checkout code
75+
uses: actions/checkout@v7
76+
77+
- name: Setup Rust
78+
# Pinned to match the Tests workflow. A different toolchain hashes into
79+
# a different cache key, which would warm a cache nothing else reads.
80+
uses: dtolnay/rust-toolchain@1.95.0
81+
with:
82+
components: clippy,rustfmt
83+
84+
- name: Cache Rust build
85+
uses: Swatinem/rust-cache@v2
86+
with:
87+
workspaces: packages/browseros-agent
88+
# Must match the Tests workflow exactly. Without it the key embeds
89+
# GITHUB_JOB and this cache would be unreadable there.
90+
shared-key: browseros-agent
91+
cache-directories: ~/.cargo/build/browseros
92+
# Save even though nothing here is a pass/fail gate; warming is the
93+
# entire point of the job.
94+
save-if: "true"
95+
96+
- name: Warm Rust cache
97+
# Mirrors what the Rust suites compile: test binaries and clippy's
98+
# separate artifacts. -D warnings is deliberately omitted, since this
99+
# job exists to populate a cache, not to gate on lints.
100+
run: |
101+
cargo test --workspace --locked --no-run
102+
cargo clippy --workspace --all-targets --locked

0 commit comments

Comments
 (0)