perf: Memoize Cargo.lock closures and share resolution identities (#14498)
Once `cargo metadata` returns, Cargo discovery turns `Cargo.lock` into
each crate's external-dependency resolution, and that work sits on the
critical path. This repository has 82 workspace members and 899 lock
entries, and the closures add up to about 33k entries. Three costs
dominated:
- The old `cargo_external_closures` ran a full traversal per member,
re-parsing and re-resolving every dependency string on every visit, with
a SipHash visited set.
- It cloned a fresh identity for each closure entry.
- The consumer then rehashed each closure into another set and cloned
every entry again into a workspace-wide union. It also allocated two
`Arc<str>` per entry before sorting.
This PR resolves each lock entry's dependencies once and builds each
identity once, then shares both by index.
```diff
cargo_external_closures(lock, members)
for member in members
- for idx in dfs(member) # re-resolves every dependency string
- closure.insert(lock[idx].identity()) # clone per entry
+ for idx in dfs(member, memoized_edges) # each entry's edges resolved once
+ closure.push(identity_slot(idx)) # identity built once, shared by index
CargoContributor::discover_packages
for crate in crates
- set = closure ∪ {rustc} # rehash + clone
- resolution = sort(set.map(new identity))
+ resolution = sorted_identities[sort(ranks(closure))] + rustc
- workspace = HashSet(all closures, cloned) ∪ {rustc}
+ workspace = bitset union over closure indices
```
- The new `LockIndex::reachable_memoized` visits entries in the same
depth-first order as before. It caches an entry's resolved edges only
after all of them resolve, so it fails on the same dependency string and
returns the same error.
- The new `cargo_external_closure_indices` returns each distinct
identity once, plus each member's closure as indices.
`cargo_external_closures` is now a thin wrapper around it with unchanged
output.
- A new `ResolutionIdentities` builds every `ExternalPackageIdentity`
once and ranks them with a single sort. It then builds each crate's list
from its sorted ranks plus the `rustc` identity.
`PackageResolution::new` still sorts and deduplicates, so every
resolution list stays identical to `main`.
Computed in isolation on this repository's `Cargo.lock` (macOS, release
build), the closures drop from 18.0 ms to 4.9 ms per call. 1.5 ms of the
4.9 ms is TOML parsing.
## Measurements
`release-turborepo` builds of `main` and this branch, on Linux x86_64
with 8 vCPU and 16 GB. Each comparison is `hyperfine -N` with 4 balanced
AB/BA blocks, 3 warmups per block, and N runs per binary per block. Both
binaries were freshly copied and the page cache dropped before
measuring. Results are the median speedup per block, with a bootstrap
95% CI.
Workloads:
- This repository, `vercel/turborepo` at `2935202280` (the base of this
PR): a Cargo workspace (82 crates) plus a pnpm workspace.
- The JavaScript-only repositories `t3-oss/create-t3-turbo@8f945b7b` and
`calcom/cal.com@176037d0`, as regression checks. These never construct
the Cargo contributor, so none of the changed code runs there.
| Repository | Command | N | `main` | Branch | Speedup | 95% CI | Blocks
|
|---|---|---|---|---|---|---|---|
| vercel/turborepo | `turbo run build --dry=json` | 20 | 1057.86 ms |
1011.96 ms | +4.55% | [+2.89%, +6.44%] | +4.3%, +4.4%, +2.4%, +7.2% |
| t3-oss/create-t3-turbo (pass 1) | `turbo run build --dry=json` | 30 |
38.60 ms | 37.42 ms | +3.60% | [+1.32%, +7.12%] | +0.9%, +2.9%, +8.9%,
+1.7% |
| t3-oss/create-t3-turbo (pass 2) | `turbo run build --dry=json` | 30 |
37.63 ms | 36.60 ms | +2.22% | [-0.68%, +5.13%] | -2.2%, +0.9%, +4.0%,
+6.2% |
| calcom/cal.com (pass 1) | `turbo run build --dry=json` | 30 | 119.03
ms | 122.59 ms | -2.60% | [-3.75%, -1.15%] | -0.5%, -2.7%, -4.1%, -3.1%
|
| calcom/cal.com (pass 2) | `turbo run build --dry=json` | 30 | 119.25
ms | 121.16 ms | -0.92% | [-4.26%, +3.62%] | -2.2%, +5.5%, -5.0%, -2.1%
|
The changed code never runs for t3 or cal.com, so their differences, in
both directions, come from noise or code layout. The first cal.com pass
looked slower, so I re-measured both repositories from fresh binary
copies and a dropped page cache. That slowdown did not reproduce.
## Manual verification
- The `--dry=json` output (with `id` removed) and stderr are
byte-identical to `main` for `turbo run build` and `turbo run lint` in
this repository. That includes every Cargo task hash, and those hashes
depend on these resolutions.
- For all 82 members of this repository's `Cargo.lock`, the memoized
closures equal the closures from the previous per-member traversal.
- Cargo failure paths match `main` in exit code, output, and stderr:
- Stale `Cargo.lock`: the conservative-hashing fallback.
- Missing `Cargo.lock`.
- Missing `[workspace.metadata] name`.