turbo-tasks-backend: remove keyed dependency edges per task in batches (#101734)
## What?
Batch outdated keyed-edge cleanup by owner and dependent, so each task
is locked once per batch rather than once per edge. This PR is based on
#101732, ported onto canary's flat `EdgeSet` storage. #101733's
implementation was superseded by #101489; its useful tests moved into
#101732. #101736 is independent.
## Why?
Readers often switch thousands of keys of the same cell. Removing each
old edge individually repeatedly acquires the same owner and reader
locks. Grouping the removals materially reduces range-switch latency.
## How?
Collect the queued keyed edges, sort/group by owner and remove the owner
sides using canary's `remove_keyed_cell_dependent`, then sort/group by
dependent and remove its current/outdated forward sides. As before,
owner sides are removed before dependent sides. The cleanup never holds
two task guards simultaneously.
Adds tests for switching key ranges, exact reverse-edge cleanup, and GC
collection of readers while the owner remains live.
## Benchmarks
Release `keyed_deps`, N=1000 × M=10,000, shared layout, 3 interleaved
blocks per thread count. Measured base: `cad154f1` (includes #101489).
The later rebase to `dcb536d0` did not change the affected crates; gates
were rerun. C includes A. Ratios are median **paired** speedups
[min–max], so need not exactly equal ratios of median times.
| Mode | Threads | Step | Base ms | A ms | A+C ms | C vs A | A+C vs base
|
|---|---:|---|---:|---:|---:|---|---|
| keyed_reader | 16 | initial | 929 | 424 | 403 | 1.05× [1.05–1.10] |
2.20× [2.10–2.51] |
| keyed_reader | 32 | initial | 928 | 455 | 421 | 1.00× [0.91–1.14] |
2.24× [1.85–2.65] |
| keyed_reader | 16 | change_all | 33494 | 34388 | 33618 | 1.02×
[0.99–1.04] | 1.01× [0.83–1.01] |
| keyed_reader | 32 | change_all | 33473 | 33545 | 33249 | 1.01×
[0.94–1.05] | 1.01× [0.94–1.18] |
| keyed_reader | 16 | change_shift | 14203 | 10980 | 5900 | **2.07×
[1.68–2.68]** | **2.43× [2.19–3.06]** |
| keyed_reader | 32 | change_shift | 14210 | 10992 | 4361 | **2.45×
[1.95–2.95]** | **3.15× [2.52–3.32]** |
| keyed_reader | 16 | change_shift (2nd) | 13081 | 10207 | 4867 |
**2.24× [2.04–2.88]** | **2.84× [2.61–3.17]** |
| keyed_reader | 32 | change_shift (2nd) | 11323 | 10761 | 3637 |
**2.96× [2.25–3.67]** | **3.54× [2.71–3.62]** |
| get | 16 | initial | 8776 | 7904 | 8737 | 0.90× [0.85–1.01] | 1.00×
[0.86–1.06] |
| get | 32 | initial | 9797 | 9503 | 9997 | 1.02× [0.95–1.04] | 1.04×
[0.93–1.16] |
| get | 16 | change_all | 42986 | 40084 | 41685 | 0.96× [0.92–1.08] |
1.04× [0.99–1.09] |
| get | 32 | change_all | 42976 | 40683 | 41694 | 0.97× [0.95–0.98] |
1.00× [0.99–1.04] |
| get | 16 | change_shift | 25598 | 23278 | 15282 | **1.51×
[1.50–1.54]** | **1.67× [1.52–1.71]** |
| get | 32 | change_shift | 25642 | 25455 | 16520 | **1.54×
[1.53–1.70]** | **1.55× [1.53–1.64]** |
| get | 16 | change_shift (2nd) | 24976 | 22462 | 14032 | **1.55×
[1.54–1.61]** | **1.78× [1.59–1.91]** |
| get | 32 | change_shift (2nd) | 26666 | 23820 | 14976 | **1.59×
[1.57–1.93]** | **1.58× [1.58–1.78]** |
Other paired medians (16 / 32 threads):
| Mode / phase | C vs A | A+C vs base |
|---|---|---|
| keyed_reader / change_all (2nd) | 1.00× / 1.01× | 1.02× / 1.01× |
| keyed_reader / change_unread | 0.99× / **0.77×** | 1.19× / **0.76×** |
| get / change_all (2nd) | 1.03× / 1.01× | 1.04× / 1.02× |
| get / change_unread | 0.97× / 0.97× | 0.98× / 1.04× |
The 32-thread keyed_reader unread-key result is slower in every pair (C
vs A: 0.70–0.99×), despite no reader cleanup occurring in that phase.
The cause is not attributed. This PR does not fix canary's `change_all`
scan regression; #101736 does that separately.
### Memory trade-off
Single samples, allocator-committed bytes per edge (**not live edge
allocations**), after the second `change_shift`:
| Layout | Threads | Mode | Base | A | A+C | C−A |
|---|---:|---|---:|---:|---:|---:|
| shared | 16 | keyed_reader | 95.6 | 95.5 | 95.6 | +0.1 |
| shared | 32 | keyed_reader | 122.3 | 118.4 | 128.9 | **+10.5** |
| shared | 16 | get | 94.8 | 95.2 | 95.6 | +0.4 |
| shared | 32 | get | 118.6 | 118.7 | 134.9 | **+16.2** |
| disjoint | 16 | keyed_reader | 149.9 | 123.1 | 140.3 | +17.2 |
| disjoint | 32 | keyed_reader | 156.1 | 149.8 | 179.4 | **+29.6** |
| disjoint | 16 | get | 136.9 | 137.3 | 136.8 | −0.5 |
| disjoint | 32 | get | 144.3 | 147.7 | 148.3 | +0.6 |
Shared peak RSS at 32 threads rises from 0.89 to 1.03 GiB
(`keyed_reader`) and 0.89 to 1.07 GiB (`get`) over A. Disjoint peak RSS
stays ~1.2 GiB at 16 threads / ~1.3 GiB at 32 threads.
This is a measured speed/memory trade-off, not a memory-neutral change.
Temporary buffers, worker-heap retention and EdgeSet capacities have not
been separately attributed; the samples do not establish that any one of
them explains it.
## Validation
- fmt; clippy `-D warnings`, including `task_dirty_cause`.
- 628 tests pass across `turbo-tasks` and `turbo-tasks-backend`.
- Mutations dropping owner-side or dependent-side cleanup are caught.
- All timed benchmark steps assert outputs and reader rerun counts.
- The GC test exercises collection cleanup but does not prove all
concurrent GC/eviction interleavings. No real-build or persistence
measurement is claimed.
<!-- NEXT_JS_LLM -->
<!-- fleet 86487b82-3b2f-4464-84da-b3be3db57b3a -->
Co-authored-by: vercel-fleet-prod[bot] <318278635+vercel-fleet-prod[bot]@users.noreply.github.com>
Co-authored-by: Tobias Koppers <1365881+sokra@users.noreply.github.com>