turbo-tasks: batch schedule invalidated tasks and cell dependents (#100446)
### What? Tasks that become dirty together are handed to the turbo-tasks scheduler (`PriorityRunner`) as one batch instead of one by one, at two sites: - `invalidate()` (explicit invalidation of a set of tasks) - `invalidate_cell_dependents()` in `update_cell` (all dependents of a changed cell — the main fan-out path) Also adds a `cell_fan_out` criterion benchmark to `turbo-tasks-backend`. ### Why? `PriorityRunner` keeps all non-lowest-priority work in a `Mutex<BinaryHeap>`. Scheduling N tasks one by one locks that heap up to N times and grows it incrementally, contending with the workers that pop from it. This is a first step towards reducing that contention. Note: in-tree, `invalidate()` is currently only reached with a single task (`TurboTasks::invalidate` → `Backend::invalidate_task`); `invalidate_tasks` / `invalidate_tasks_set` have no in-tree callers. The real fan-out is a cell change invalidating its dependents, which is why `update_cell` is batched too. ### How? - **`PriorityRunner::schedule_many`**: behaves like calling `schedule` for each item. While there is free worker capacity, items are handed directly to new workers using the same slot reservation as `schedule`. The remaining items are queued in bulk: each item is still inserted into the claimable map before its key is published, lowest-priority keys still go to the lock-free queue, and all other keys are staged and then pushed into the heap under a single lock acquisition after one `reserve`. The staging buffer is only allocated when a batch actually contains heap items. Each claimable insert finishes before the heap is locked, preserving the existing heap → claimable lock order. - The liveness protocol is unchanged: after the bulk push there is one `SeqCst` fence followed by an `active_workers` re-check. If capacity opened up concurrently, workers are spawned for queued work as long as capacity remains. Items replaced by a newer item with the same claim key are still passed to `Executor::discard`. - **`TurboTasks::schedule_many`**: batch counterpart of `TurboTasks::schedule`; begins all foreground jobs and updates the scheduled-task counter up front; no-op for an empty batch. - **Backend**: the dirty-marking logic was split into `make_task_dirty_without_scheduling`, which returns the parent priority instead of scheduling. `invalidate()` and `invalidate_cell_dependents()` compute each task's scheduling priority while holding its guard, collect into a pre-reserved buffer, and schedule once after all tasks are marked dirty. In `update_cell` the existing order (remove old content → dirty dependents → set new content → notify waiters → run aggregation updates) is preserved; only the scheduling is deferred to the end of the dirtying loop. `make_task_dirty_internal` keeps scheduling immediately for its other callers (aggregation update, connect child(ren)). New `priority_runner` unit tests cover mixed priority bands, interleaving with `schedule`, empty batches, claiming batched items, duplicate claim keys, a multi-worker run where every item executes exactly once, and diagnostics counting. ### Benchmark `cargo bench -p turbo-tasks-backend --bench mod -- cell_fan_out` Graph: a `State` → one `source` task producing a cell → N `dependent` tasks reading that cell → a root summing them. The graph is built once (untimed); each timed iteration changes the state and does a strongly consistent read of the root, i.e. invalidation fan-out + scheduling + re-execution of all N dependents. The result is asserted every iteration. A/B on one 8-vCPU x86_64 Linux VM (Intel Xeon @ 2.50 GHz), release build, in-memory backend. Base is the parent commit of this PR with the same benchmark file added; six alternating base/branch process runs, values are criterion's central time estimate per run (ms): | N | base (6 runs) | branch (6 runs) | median base → branch | paired change (approx. 95% CI) | |---|---|---|---|---| | 1,000 | 6.45, 6.14, 5.96, 6.54, 5.69, 6.57 | 7.12, 7.15, 5.53, 5.77, 6.06, 5.29 | 6.29 → 5.91 | −1.7% [−15.5%, +14.4%] | | 10,000 | 73.49, 75.38, 89.11, 72.21, 69.77, 65.11 | 68.38, 76.65, 64.04, 75.06, 66.85, 68.76 | 72.85 → 68.57 | −5.4% [−18.6%, +9.9%] | | 100,000 | 864.7, 1011.5, 953.4, 883.8, 856.5, 814.3 | 848.5, 915.0, 791.9, 876.6, 928.1, 848.1 | 874.3 → 862.6 | −3.2% [−12.5%, +7.2%] | **No detected end-to-end difference**: the branch tends to be slightly faster, but every interval overlaps zero and run-to-run noise (~5–10%) is larger than the effect. Re-executing the N dependents (~8–9 µs per task here) dominates each iteration, so the scheduler's heap lock is a small share of the measured time. The intervals only reflect repeated processes on one VM (not independent machines), and runs were always base-first. The reduced number of heap lock acquisitions follows from the code structure; it is not separately instrumented. The benchmark is kept so later steps (batching more scheduling sites, reducing heap contention further) can be measured against it. ### Verification - `cargo test -p turbo-tasks --lib priority_runner` (also with `--features inline_execution_stats`) - `cargo test -p turbo-tasks-backend` - `cargo clippy -p turbo-tasks -p turbo-tasks-backend --all-targets -- -D warnings` <!-- NEXT_JS_LLM --> <!-- fleet d21ddb86-de88-41df-82c7-aa6a29a6165e --> --------- 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>
Showing
This diff is collapsed.
Please
register
or
sign in
to comment