Unverified Commit 2420b9f5 authored by Tobias Koppers's avatar Tobias Koppers Committed by GitHub

perf(turbopack): look up chunk group indices through a keyed cell (#99050)

### What?

Narrows the turbo-tasks dependency of `ChunkGroupInfo::get_index_of` so
that looking up one chunk group's index no longer depends on the entire
`ChunkGroupInfo`.

This is PR 6 of a six-PR stack, based on `canary` (#98733, #99686 and
#99749 are already merged):

1. #98823 — Pages Router dev client graph chaining
2. #99910 — regression test for Pages Router `_app` availability
(#99789)
3. #98734 — use graph-derived merged groups
4. #99750 — chunking takes a `ChunkGroupKey`
5. #98685 — chunk-group availability bitmaps
6. **This PR** — keyed chunk-group index lookups

### Why?

#98685 makes availability a set of chunk-group *indices*, which turns
`get_index_of` into a hot path: every availability translation between
graphs resolves keys to indices through it.

That lookup previously read `chunk_group_keys` off the whole
`ChunkGroupInfo` value. In turbo-tasks, reading one field of a cell
creates a dependency on the cell as a whole, so every index lookup in
the graph was invalidated whenever *any* unrelated part of the chunk
group info changed — a new async chunk group anywhere in the app re-ran
every lookup, and everything downstream of them. The wider the
availability work built on top of it, the more that costs.

### How?

Adds `ChunkGroupKeyToIndex`, a `cell = "keyed"` map from `ChunkGroupKey`
to its index, alongside the existing set, exposed through an
argument-less `chunk_group_indices()` task. `get_index_of` reads a
single key from it through `Vc::get`, so its caller depends only on the
key it actually asked for: an unrelated chunk group changing no longer
invalidates it.

`get_index_of` itself is deliberately a plain async method rather than a
turbo-tasks function. As a function it would create one task per chunk
group argument, which is exactly the per-chunk-group overhead this
change is meant to remove; the single `chunk_group_indices()` task is
what provides the caching.

The interesting constraint is why this can't reuse the
`FxIndexSet<ChunkGroupKey>` that was already there — a set that already
holds exactly these keys in exactly this order. Two properties of the
keyed-cell traits rule it out:

- `KeyedAccess for IndexSet` yields `()`, not the index, so the value
needed isn't retrievable through a keyed read.
- `KeyedEq for IndexSet` compares *membership* and ignores position. A
key whose index merely shifted — same set, different order — would not
be reported as changed, and lookups would keep returning a stale index.
That is a correctness bug, not just a missed invalidation.

Mapping the key to the index as the *value* makes `KeyedEq for HashMap`
compare indices, which detects exactly that shift. The `FxIndexSet`
stays for the reverse (index → key) direction, so the duplication is
deliberate and documented at the type.

The debug-only "couldn't find chunk group" diagnostic still reads the
whole struct to render its message. That path is only taken on failure,
so the happy path keeps its narrow dependency.

### Verification

Keyed-cell invalidation semantics (per-key dependencies, `KeyedEq`
comparing values) are covered by turbo-tasks' own tests, so this PR does
not add a dedicated invalidation test.

- `cargo clippy --workspace --all-targets -- -D warnings -A deprecated`
- `turbopack-core` + `next-core` — 252 passed
- Turbopack snapshots — 126 passed, no output churn
- `cargo fmt --check` clean

Behavior is unchanged; this is a dependency-graph refactor with no
effect on emitted output.


<!-- fleet 4f67c6f7-bbe2-454b-9a17-822a60ef8f7e -->
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 9ffb2fce
...@@ -1969,7 +1969,7 @@ impl AppEndpoint { ...@@ -1969,7 +1969,7 @@ impl AppEndpoint {
let derived_group = if !has_server_utils { let derived_group = if !has_server_utils {
None None
} else { } else {
let entry_index = *chunk_group_info let entry_index = chunk_group_info
.get_index_of(entry_chunk_group.clone()) .get_index_of(entry_chunk_group.clone())
.await?; .await?;
let merged = chunk_group_info let merged = chunk_group_info
......
...@@ -48,7 +48,7 @@ impl AvailabilityInfo { ...@@ -48,7 +48,7 @@ impl AvailabilityInfo {
module_graph: ResolvedVc<ModuleGraph>, module_graph: ResolvedVc<ModuleGraph>,
chunk_group: ChunkGroupKey, chunk_group: ChunkGroupKey,
) -> Result<Self> { ) -> Result<Self> {
let chunk_group = *module_graph let chunk_group = module_graph
.chunk_group_info() .chunk_group_info()
.get_index_of(chunk_group) .get_index_of(chunk_group)
.await? as u32; .await? as u32;
......
...@@ -313,7 +313,7 @@ async fn chunk_group_content_operation( ...@@ -313,7 +313,7 @@ async fn chunk_group_content_operation(
let chunk_group_info = module_graph.chunk_group_info(); let chunk_group_info = module_graph.chunk_group_info();
let chunk_group_indices = chunk_groups let chunk_group_indices = chunk_groups
.iter() .iter()
.map(async |chunk_group| Ok(*chunk_group_info.get_index_of(chunk_group.key()).await?)) .map(async |chunk_group| chunk_group_info.get_index_of(chunk_group.key()).await)
.try_join() .try_join()
.await?; .await?;
let chunk_group_info = chunk_group_info.await?; let chunk_group_info = chunk_group_info.await?;
......
...@@ -113,6 +113,15 @@ pub struct ChunkGroups(Vec<ChunkGroupWithIndex>); ...@@ -113,6 +113,15 @@ pub struct ChunkGroups(Vec<ChunkGroupWithIndex>);
#[turbo_tasks::value(transparent)] #[turbo_tasks::value(transparent)]
pub struct OptionChunkGroupWithIndex(Option<ChunkGroupWithIndex>); pub struct OptionChunkGroupWithIndex(Option<ChunkGroupWithIndex>);
/// Maps each [`ChunkGroupKey`] to its index in [`ChunkGroupInfo::chunk_groups`], as a keyed cell so
/// [`ChunkGroupInfo::get_index_of`] depends only on the key it looks up.
///
/// This duplicates [`ChunkGroupInfo::chunk_group_keys`], which stays for the index -> key
/// direction. The `IndexSet` can't be the keyed cell: its `KeyedAccess` yields `()` rather than the
/// index, and its `KeyedEq` ignores position, so an index that merely shifted would go unnoticed.
#[turbo_tasks::value(transparent, cell = "keyed")]
pub struct ChunkGroupKeyToIndex(FxHashMap<ChunkGroupKey, u32>);
#[turbo_tasks::value] #[turbo_tasks::value]
pub struct ChunkGroupInfo { pub struct ChunkGroupInfo {
pub module_chunk_groups: ResolvedVc<ModuleToChunkGroups>, pub module_chunk_groups: ResolvedVc<ModuleToChunkGroups>,
...@@ -123,6 +132,9 @@ pub struct ChunkGroupInfo { ...@@ -123,6 +132,9 @@ pub struct ChunkGroupInfo {
#[turbo_tasks(unsafe_ignore)] #[turbo_tasks(unsafe_ignore)]
#[bincode(with = "turbo_bincode::indexset")] #[bincode(with = "turbo_bincode::indexset")]
pub chunk_group_keys: FxIndexSet<ChunkGroupKey>, pub chunk_group_keys: FxIndexSet<ChunkGroupKey>,
/// The same keys as `chunk_group_keys`, mapped to their index, in a keyed cell so a single
/// index lookup does not depend on the whole `ChunkGroupInfo`. See [`ChunkGroupKeyToIndex`].
pub chunk_group_indices: ResolvedVc<ChunkGroupKeyToIndex>,
pub chunking_heuristics: ChunkingHeuristicsInfo, pub chunking_heuristics: ChunkingHeuristicsInfo,
} }
...@@ -152,28 +164,11 @@ impl ChunkGroupInfo { ...@@ -152,28 +164,11 @@ impl ChunkGroupInfo {
*self.module_chunk_groups *self.module_chunk_groups
} }
/// Returns the index of the chunk group identified by `key` in /// The keyed `ChunkGroupKey` -> index map. Read a single entry from it with [`Vc::get`] to
/// [`ChunkGroupInfo::chunk_groups`]. /// depend only on that key rather than on all of [`ChunkGroupInfo`].
#[turbo_tasks::function] #[turbo_tasks::function]
pub async fn get_index_of(&self, key: ChunkGroupKey) -> Result<Vc<usize>> { pub fn chunk_group_indices(&self) -> Vc<ChunkGroupKeyToIndex> {
if let Some(idx) = self.chunk_group_keys.get_index_of(&key) { *self.chunk_group_indices
Ok(Vc::cell(idx))
} else {
if cfg!(debug_assertions) {
bail!(
"Couldn't find chunk group index for {} in {}",
key.debug_str(&self.chunk_group_keys).await?,
self.chunk_groups
.iter()
.map(|c| c.debug_str(self))
.try_join()
.await?
.join(", ")
);
} else {
bail!("Couldn't find chunk group index")
}
}
} }
/// Returns the exact chunk groups that contain `module`, as derived while traversing the module /// Returns the exact chunk groups that contain `module`, as derived while traversing the module
...@@ -225,12 +220,43 @@ impl ChunkGroupInfo { ...@@ -225,12 +220,43 @@ impl ChunkGroupInfo {
} }
impl ChunkGroupInfo { impl ChunkGroupInfo {
/// Returns the index of the chunk group identified by `key` in
/// [`ChunkGroupInfo::chunk_groups`].
///
/// The lookup goes through the keyed [`ChunkGroupKeyToIndex`] cell, so the caller depends only
/// on the queried key: adding or changing an unrelated chunk group does not invalidate it.
///
/// This is deliberately *not* a `turbo_tasks::function`. Making it one would create a separate
/// task per key argument, which is exactly the per-chunk-group overhead the keyed cell is meant
/// to avoid; the single argument-less [`ChunkGroupInfo::chunk_group_indices`] task is what
/// provides the caching.
pub async fn get_index_of(self: Vc<Self>, key: ChunkGroupKey) -> Result<usize> {
if let Some(idx) = self.chunk_group_indices().get(&key).await? {
Ok(*idx as usize)
} else if cfg!(debug_assertions) {
// Only taken on the failure path, so the happy path keeps its narrow dependency.
let this = self.await?;
bail!(
"Couldn't find chunk group index for {} in {}",
key.debug_str(&this.chunk_group_keys).await?,
this.chunk_groups
.iter()
.map(|c| c.debug_str(&this))
.try_join()
.await?
.join(", ")
)
} else {
bail!("Couldn't find chunk group index")
}
}
/// Returns the registered chunk group identified by `key`. /// Returns the registered chunk group identified by `key`.
/// ///
/// Chunking takes a [`ChunkGroupKey`] and resolves the group here, so a group's contents (e.g. /// Chunking takes a [`ChunkGroupKey`] and resolves the group here, so a group's contents (e.g.
/// the entries of a merged group) always come from the module graph rather than the caller. /// the entries of a merged group) always come from the module graph rather than the caller.
pub async fn get_chunk_group(self: Vc<Self>, key: ChunkGroupKey) -> Result<ChunkGroup> { pub async fn get_chunk_group(self: Vc<Self>, key: ChunkGroupKey) -> Result<ChunkGroup> {
let index = *self.get_index_of(key).await?; let index = self.get_index_of(key).await?;
Ok(self.await?.chunk_groups[index].clone()) Ok(self.await?.chunk_groups[index].clone())
} }
} }
...@@ -1018,6 +1044,13 @@ pub async fn compute_chunk_group_info(graph: &ModuleGraph) -> Result<Vc<ChunkGro ...@@ -1018,6 +1044,13 @@ pub async fn compute_chunk_group_info(graph: &ModuleGraph) -> Result<Vc<ChunkGro
module_chunk_groups: ResolvedVc::cell(module_chunk_groups), module_chunk_groups: ResolvedVc::cell(module_chunk_groups),
async_loader_chunk_groups: ResolvedVc::cell(async_loader_chunk_groups), async_loader_chunk_groups: ResolvedVc::cell(async_loader_chunk_groups),
chunk_group_keys: chunk_groups_map.keys().cloned().collect(), chunk_group_keys: chunk_groups_map.keys().cloned().collect(),
chunk_group_indices: ResolvedVc::cell(
chunk_groups_map
.keys()
.enumerate()
.map(|(index, key)| (key.clone(), index as u32))
.collect(),
),
chunking_heuristics: ChunkingHeuristicsInfo { chunking_heuristics: ChunkingHeuristicsInfo {
clusters: chunk_group_clusters, clusters: chunk_group_clusters,
priority_routes: chunk_group_priority_routes, priority_routes: chunk_group_priority_routes,
......
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