turbo-tasks-backend: inflate large dependency sets before invalidation lookups (#102306)
### What?
Makes invalidating many cells that one task depends on fast again. When
a task read many cells (e.g. a task awaiting many tasks) and many of
those cells change before it executes again,
`invalidate_cell_dependents` now does a hash lookup in that task's
dependency set instead of a linear scan per changed cell.
### Why?
A completed task keeps its `cell_dependencies` in a flat `EdgeSet`
(#101489), whose `contains` is a linear scan.
`invalidate_cell_dependents` checks each dependent with
`outdated_cell_dependencies_contains` (hashed) and then
`cell_dependencies_contains`. The dependencies are only copied to the
hashed outdated set when the dependent starts executing again. Until
then, each changed cell scans the dependent's flat set while holding its
lock, so N changed cells cost O(N²) in total.
This shows up in the `turbo_tasks_backend_cell_fan_out` bench at width
100k. A `perf` profile attributes most `update_cell` samples to this
scan (`slice::contains` ← `EdgeSet::contains` ←
`cell_dependencies_contains` ← `invalidate_cell_dependents`).
### How?
Before the current-set lookup (only when the dependency isn't in the
outdated set), call the already-generated `cell_dependencies_inflate()`
/ `cell_dependencies_keyed_inflate()`:
- a flat set of more than 32 entries is converted to a hash table once,
and the following lookups are hash lookups;
- smaller sets stay flat;
- the provisional branch is unchanged.
**Trade-off:** the inflated table is kept until the task completes
again, which flattens it. That includes tasks whose dependency turns out
to be removed (they aren't made dirty) and dirty tasks that don't
execute soon.
### Verification
**Tests:**
- `cargo test -p turbo-tasks-backend -p turbo-tasks` (debug): passes, 70
test targets.
- `cargo test -p turbo-tasks-backend --release`: only
`test_eventual_read_in_top_level_task_fails` fails, and it fails the
same way on the base `53de7e1a`.
- 10 release repetitions of `--lib --test invalidation --test
keyed_dependencies --test keyed_many_changed_keys` (existing tests,
including the `invalidate_cell_dependents` unit tests): 10/10 pass.
- `cargo clippy -p turbo-tasks-backend --all-targets -- -D warnings` and
`cargo fmt --check` pass.
**Benchmarks:** base `53de7e1a` vs this branch, n = 4 per cell,
alternating order, pinned CPUs. Ratio = head / base, mean (min–max)
[pairs > 1]:
| | 16 threads | 32 threads |
|---|---|---|
| `cell_fan_out` width 100k | 2053 → 755 ms, 0.47 (0.25–0.69) [0/4] |
4095 → 766 ms, 0.19 (0.17–0.21) [0/4] |
| `cell_fan_out` width 10k | 0.99 (0.93–1.12) [1/4] | 1.01 (0.90–1.12)
[2/4] |
| `cell_fan_out` width 1k | 1.12 (0.96–1.28) [2/4] | **1.15 (1.04–1.26)
[4/4]**, about +0.8 ms |
| `cell_fan_out` peak RSS | 1.03 (1.00–1.06) [3/4] | **1.14 (1.09–1.18)
[4/4]** |
| keyed_deps peak RSS (`keyed_reader` / `get`) | 1.01 / 1.04 [4/4] |
1.03 / 1.03 [4/4] |
- **Width 1k at 32 threads** is slower in 4/4 pairs. Its 1000-entry set
exceeds the 32-entry threshold and gets inflated, so inflation cost is a
plausible cause, but it isn't established.
- **keyed_deps phase times** (n = 1000, m = 10000): no consistent
direction. A few cells move in 4/4 pairs, but opposite ways at 16 and 32
threads: `keyed_reader` initial 1.11 at 16 vs 0.87 at 32, and change_all
1.04 vs 0.88.
- **Real build** (`bench/large-app-router`, cold, 32 threads, n = 4,
alternating):
| | base | this branch | ratio |
|---|---|---|---|
| wall | 85.7 s [77.4–88.9] | 79.4 s [74.6–87.9] | 0.93 (0.84–0.99)
[0/4] |
| compile | 56.1 s [49.9–59.3] | 50.4 s [46.8–54.5] | 0.90 (0.79–0.95)
[0/4] |
| main process VmHWM | 11.40 GiB | 11.35 GiB | 1.00 (0.97–1.01) [2/4] |
| process tree peak RSS | 18.25 GiB [17.04–21.74] | 17.11 GiB
[17.05–17.16] | 0.95 (0.78–1.00) [3/4] |
Ranges overlap. The real build wasn't profiled, so whether this path
explains its difference isn't established.
- **Not run:** dev / HMR workloads, other apps, more than n = 4.
<!-- NEXT_JS_LLM -->
<!-- fleet 7d772cb1-d32e-432c-af7b-13d4bff54c18 -->
Co-authored-by: vercel-fleet-prod[bot] <318278635+vercel-fleet-prod[bot]@users.noreply.github.com>
Co-authored-by: vercel-fleet <fleet@vercel.invalid>
Co-authored-by: Tobias Koppers <1365881+sokra@users.noreply.github.com>