Unverified Commit a71504e5 authored by Tobias Koppers's avatar Tobias Koppers Committed by GitHub

turbo-tasks-backend: optimize aggregated updates outside of the lock (#100165)

### What?

Net the collectible counts of an `AggregatedDataUpdate` in a new
`AggregatedDataUpdate::optimize()`, called after the follower task's
lock has been released.

### Why?

When an aggregating task gains (or loses) an upper,
`AggregatedDataUpdate::from_task` builds an update from the task's own
collectible counts plus `+1` for every positive aggregated collectible.

- **Cancelling entries:** a task that takes or drops the collectibles of
its subgraph holds an own `-1` for each of them, so most of those
entries cancel out. An earlier large `next build` investigation found
one such update with 106,268 entries, of which only 4 had a nonzero net
count.
- **Serialization:** in a large `next build`, ~6,000 uppers connect the
same task. `from_task` runs while that follower's lock is held, so
anything done per update while the lock is held serializes across all
workers.
- **Why the earlier version didn't help:** a previous version of this PR
netted inside `from_task`, still under the lock. It didn't improve emit
time.

### How?

- **`from_task`:** back to its original form (own entries, then `+1` per
positive aggregated entry). It keeps a `Vec::with_capacity` upper bound
so the vector isn't reallocated while the task is locked.
- **`optimize()`:** sums the collectible entries per collectible, drops
zero sums, and keeps the dirty container update unchanged. It reuses the
vector's allocation.
- **Call sites:** `optimize()` is called right after the follower lock
is dropped, at the sites that apply the update to uppers afterwards.
These are the new-follower paths and the lost-follower paths. In the
lost-follower paths, `invert()` also moved out of the lock, after
`optimize()`. The `balance_edge` paths, which hold both locks through
`apply`, are unchanged.
- **Semantics:** applying the optimized update (or its inversion) gives
the same final counts in the upper, and the same set of collectibles
whose presence changes. Intermediate flips within one update can be
skipped. Result order is unspecified.

**Tests:**
- Unit tests for summing, cancellation, mixed counts, empty and single
entries, and disjoint keys.
- Equivalence of final counts and presence changes for add and invert.
- The real `optimize().invert()` path on `CollectibleRef`s, including
the dirty container update.
- An integration test that reads an operation which takes its child's
collectibles, through several root uppers.

**Measurement** on canary `8521c6ae`
(`bench/nested-deps-app-router-many-pages`, 32 CPUs, cold untraced
builds, 1 warm-up plus 4 interleaved rounds, median of the paired
per-round changes vs canary):

| | change |
|---|---:|
| `turbopack-emit` | **−32.2%** (17.4 s → 11.9 s) |
| "Compiled in" | −11.2% |
| wall | −10.0% |

All three were faster in all 4 rounds. Output (`.next` file set) and
issues are unchanged vs canary. This is one VM and 4 rounds, and the
gain isn't broken down between the preallocation and moving work out of
the lock.

<!-- NEXT_JS_LLM -->


<!-- fleet d8fee2f9-18db-4936-b59a-f8417b8f4bbc -->

---------
Co-authored-by: 's avatarvercel-fleet-prod[bot] <318278635+vercel-fleet-prod[bot]@users.noreply.github.com>
Co-authored-by: 's avatarTobias Koppers <1365881+sokra@users.noreply.github.com>
parent 98a73ead
#![feature(arbitrary_self_types)]
#![feature(arbitrary_self_types_pointers)]
#![allow(clippy::needless_return)] // tokio macro-generated code doesn't respect this
//! A task that takes the collectibles of a child operation holds a negative own count for each of
//! them. Each upper connecting that task must still see only the collectibles that weren't taken,
//! also after the child changes.
use anyhow::Result;
use turbo_rcstr::RcStr;
use turbo_tasks::{CollectiblesSource, ResolvedVc, State, ValueToString, Vc, emit};
use turbo_tasks_testing::{Registration, register, run_once};
static REGISTRATION: Registration = register!();
const UPPERS: u32 = 8;
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn taken_collectibles_stay_hidden_from_many_uppers() {
run_once(&REGISTRATION, async || {
let input = ChangingInput {
state: State::new(20),
}
.resolved_cell();
for count in [20, 5, 30] {
input.await?.state.set(count);
for upper in 0..UPPERS {
let read = upper_operation(input, upper)
.read_strongly_consistent()
.await?;
assert_eq!(read.taken, count, "upper {upper}");
assert_eq!(read.visible, vec![format!("keep {count}")], "upper {upper}");
}
}
anyhow::Ok(())
})
.await
.unwrap()
}
#[turbo_tasks::value]
struct ChangingInput {
state: State<u32>,
}
#[turbo_tasks::value]
struct Collectible {
value: RcStr,
}
#[turbo_tasks::value_impl]
impl ValueToString for Collectible {
#[turbo_tasks::function]
fn to_string(&self) -> Vc<RcStr> {
Vc::cell(self.value.clone())
}
}
fn emit_string(value: String) {
let collectible: ResolvedVc<Box<dyn ValueToString>> = ResolvedVc::upcast(
Collectible {
value: value.into(),
}
.resolved_cell(),
);
emit(collectible);
}
#[turbo_tasks::function]
async fn emit_one(index: u32, count: u32) -> Result<()> {
emit_string(format!("child {index} of {count}"));
Ok(())
}
/// Emits `count` collectibles from separate child tasks.
#[turbo_tasks::function(operation, root)]
async fn child_operation(input: ResolvedVc<ChangingInput>) -> Result<Vc<u32>> {
let count = *input.await?.state.get();
for index in 0..count {
emit_one(index, count).await?;
}
Ok(Vc::cell(count))
}
#[turbo_tasks::function]
async fn emit_keep(count: u32) -> Result<()> {
emit_string(format!("keep {count}"));
Ok(())
}
/// Takes all collectibles of the child operation, and emits one collectible of its own.
#[turbo_tasks::function(operation, root)]
async fn taking_operation(input: ResolvedVc<ChangingInput>) -> Result<Vc<u32>> {
let child = child_operation(input);
let count = *child.connect().await?;
let taken = child.take_collectibles::<Box<dyn ValueToString>>();
emit_keep(count).await?;
Ok(Vc::cell(taken.len() as u32))
}
#[turbo_tasks::value]
struct UpperRead {
taken: u32,
visible: Vec<String>,
}
/// One of several uppers that connect the same taking operation.
#[turbo_tasks::function(operation, root)]
async fn upper_operation(input: ResolvedVc<ChangingInput>, upper: u32) -> Result<Vc<UpperRead>> {
let _ = upper;
let taking = taking_operation(input);
let taken = *taking.connect().await?;
let mut visible = Vec::new();
for collectible in taking.peek_collectibles::<Box<dyn ValueToString>>() {
visible.push(collectible.to_string().await?.to_string());
}
visible.sort();
Ok(UpperRead { taken, visible }.cell())
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment