Unverified Commit 62bf969d authored by Tobias Koppers's avatar Tobias Koppers Committed by GitHub

Turbopack: canonicalize chunk group identity (#98733)

### What?

Makes `ChunkGroupInfo` resolve chunk groups through their canonical
`ChunkGroupKey` identity.

### Why?

Merged chunk groups carry an entry list whose order is intentionally
unspecified. Looking up the full `ChunkGroup` value therefore makes
identity depend on incidental entry ordering, even though the module
graph already indexes groups by parent and merge tag.

This is PR 1 of an eight-PR stack (on top of #99100, already merged):

1. **This PR** — canonical chunk-group identity
2. #99686 — chunk several chunk groups in one `chunk_group` call
3. #99749 — register the chunk groups that are chunked
4. #98734 — use graph-derived merged groups
5. #99750 — chunking takes a `ChunkGroupKey`
6. #98823 — Pages Router dev client graph chaining
7. #98685 — chunk-group availability bitmaps
8. #99050 — keyed chunk-group index lookups

It provides stable identity for the graph-derived chunk-group work above
it.

### How?

Adds a canonical key conversion for every chunk-group variant and uses
the existing `chunk_group_keys` index for lookup. Merged keys
intentionally contain only their parent group and merge tag.

### Verification

- `cargo test -p turbopack-core` — 160 passed
- `cargo test -p turbopack-tests --test snapshot` — 125 passed
- Affected Cargo checks and Rust formatting

<!-- NEXT_JS_LLM -->


<!-- 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 4b28e261
......@@ -1976,7 +1976,7 @@ impl AppEndpoint {
let span = tracing::trace_span!("server utils");
async {
let parent_chunk_group = *chunk_group_info
.get_index_of(entry_chunk_group.clone())
.get_index_of(entry_chunk_group.key())
.await?;
let server_utils = client_references
......
......@@ -7,7 +7,10 @@ use turbo_tasks::{
use turbopack_core::{
chunk::{ChunkGroupResult, ChunkingContext, availability_info::AvailabilityInfo},
module::Module,
module_graph::{ModuleGraph, chunk_group_info::ChunkGroup},
module_graph::{
ModuleGraph,
chunk_group_info::{ChunkGroup, ChunkGroupKey},
},
output::{OutputAsset, OutputAssets, OutputAssetsWithReferenced},
};
......@@ -181,7 +184,7 @@ pub async fn get_app_client_references_chunks(
client_references_by_server_component.into_iter()
{
let parent_chunk_group = *chunk_group_info
.get_index_of(ChunkGroup::Shared(ResolvedVc::upcast(server_component)))
.get_index_of(ChunkGroupKey::Shared(ResolvedVc::upcast(server_component)))
.await?;
let base_ident = server_component.ident().owned().await?;
......
......@@ -78,10 +78,7 @@ describe('layout-segment-chunk-sharing', () => {
// bundler, not on the mode.
;(!isTurbopack ? describe.skip : describe)('chunk sharing', () => {
it('emits the shared layout module into exactly one server chunk', async () => {
// `next dev` and `next build` write their output to different trees.
const serverDir = isNextDev
? join(next.testDir, '.next', 'dev', 'server')
: join(next.testDir, '.next', 'server')
const serverDir = join(next.testDir, next.distDir, 'server')
const entries = await readdir(serverDir, {
recursive: true,
......
......@@ -119,15 +119,17 @@ impl ChunkGroupInfo {
*self.module_chunk_groups
}
/// Returns the index of the chunk group identified by `key` in
/// [`ChunkGroupInfo::chunk_groups`].
#[turbo_tasks::function]
pub async fn get_index_of(&self, chunk_group: ChunkGroup) -> Result<Vc<usize>> {
if let Some(idx) = self.chunk_groups.get_index_of(&chunk_group) {
pub async fn get_index_of(&self, key: ChunkGroupKey) -> Result<Vc<usize>> {
if let Some(idx) = self.chunk_group_keys.get_index_of(&key) {
Ok(Vc::cell(idx))
} else {
if cfg!(debug_assertions) {
bail!(
"Couldn't find chunk group index for {} in {}",
chunk_group.debug_str(self).await?,
key.debug_str(self.chunk_group_keys.clone()).await?,
self.chunk_groups
.iter()
.map(|c| c.debug_str(self))
......@@ -262,6 +264,35 @@ impl ChunkGroup {
}
}
/// Returns the canonical identity used to index this chunk group.
///
/// A merged group is identified by its parent and merge tag alone. Those two determine the
/// group; its `entries` are the *result* of merging everything reachable through the tagged
/// references, not part of its identity, and their order is unspecified. Including them would
/// make two values describing the same group compare unequal.
pub fn key(&self) -> ChunkGroupKey {
match self {
ChunkGroup::Entry(entries) => ChunkGroupKey::Entry(entries.clone()),
ChunkGroup::Async(module) => ChunkGroupKey::Async(*module),
ChunkGroup::Isolated(module) => ChunkGroupKey::Isolated(*module),
ChunkGroup::IsolatedMerged {
parent, merge_tag, ..
} => ChunkGroupKey::IsolatedMerged {
parent: ChunkGroupId::from(*parent),
merge_tag: merge_tag.clone(),
},
ChunkGroup::Shared(module) => ChunkGroupKey::Shared(*module),
ChunkGroup::SharedMultiple(entries) => ChunkGroupKey::SharedMultiple(entries.clone()),
ChunkGroup::SharedMerged {
parent, merge_tag, ..
} => ChunkGroupKey::SharedMerged {
parent: ChunkGroupId::from(*parent),
merge_tag: merge_tag.clone(),
},
ChunkGroup::Collected(module) => ChunkGroupKey::Collected(*module),
}
}
pub fn entries_count(&self) -> usize {
match self {
ChunkGroup::Async(_)
......@@ -344,6 +375,7 @@ impl ChunkGroup {
}
/// See [ChunkGroup] for documentation
#[turbo_tasks::task_input]
#[derive(Debug, Clone, Hash, PartialEq, Eq, Encode, Decode)]
pub enum ChunkGroupKey {
Entry(Vec<ResolvedVc<Box<dyn Module>>>),
......@@ -415,6 +447,7 @@ impl ChunkGroupKey {
}
}
#[turbo_tasks::task_input]
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, Encode, Decode)]
pub struct ChunkGroupId(u32);
......
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