turbo-tasks: unemit taken collectibles in a single backend operation (#100437)
### What? `take_collectibles` / `drop_collectibles` now unemit all taken collectibles in a single backend operation instead of one operation per collectible. ### Why? `TurboTasks::unemit_collectibles` looped over every collectible and called `Backend::unemit_collectible` for each. In `TurboTasksBackend` every one of those calls locked the task, built its own aggregation update queue and propagated a single-entry collectibles update through the aggregation graph. With many collectibles that means one task lock and one aggregation graph update per collectible. ### How? - New required `Backend::unemit_collectibles` trait method. `TurboTasks::unemit_collectibles` calls it once, and does nothing when no count is positive. - `TurboTasksBackend` implements it with a batched `UpdateCollectibleOperation::run_batch`: - locks the task once and checks the root-task restriction up front; - reconciles each collectible against its outdated count, with the same per-collectible semantics as before; - invalidates collectible dependents at most once per collectible type; - propagates all remaining changes as one aggregated data update, so the aggregation queue runs once. - `AggregatedDataUpdate`'s collectibles update is now a `SmallVec<[_; 1]>`, so single-collectible updates don't heap-allocate and the batch is handed over without conversion. - The single-collectible `UpdateCollectibleOperation::run` now goes through the same code path. New regression test `tests/collectibles_take_batch.rs` covers: - take and drop; - the same collectible emitted several times, from one task and from two tasks; - several uppers; - the set of collectibles changing between re-executions; - a nested take; - taking from a source with no collectibles. It passes both before and after this change. ### Benchmark New criterion benchmark `benches/collectibles.rs`. A root task takes (or peeks) N collectibles of an operation, below a chain of 8 tasks. Emission happens outside the timed region. ``` cargo bench -p turbo-tasks-backend --bench mod -- collectibles ``` Medians from two interleaved base/new rounds on one 8-core VM: | N | base | new | change | |---|---|---|---| | 1,000 | 655 µs / 575 µs | 438 µs / 384 µs | −33% / −33% | | 10,000 | 4.8 ms / 4.9 ms | 3.2 ms / 2.9 ms | −34% / −41% | For N = 10 and 100 there was no consistent difference. The VM's run-to-run noise there (±15–25%, also for the unchanged `peek` variant) is larger than any effect. ### Verification - `cargo test -p turbo-tasks -p turbo-tasks-backend` - `cargo clippy -p turbo-tasks -p turbo-tasks-backend --all-targets -- -D warnings` <!-- NEXT_JS_LLM --> <!-- fleet 53aa7530-1b3f-423b-8eb2-29bf4b657aba --> --------- 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
Please
register
or
sign in
to comment