Skip to content

Commit c8ac0bd

Browse files
authored
[Turbopack] add ChunkingType::Shared (#76228)
### What? use ChunkingType::Shared for server utils and components
1 parent 8f346de commit c8ac0bd

File tree

7 files changed

+100
-12
lines changed

7 files changed

+100
-12
lines changed

crates/next-core/src/next_server_component/server_component_reference.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ use anyhow::Result;
22
use turbo_rcstr::RcStr;
33
use turbo_tasks::{ResolvedVc, ValueToString, Vc};
44
use turbopack_core::{
5-
chunk::ChunkableModuleReference, module::Module, reference::ModuleReference,
5+
chunk::{ChunkableModuleReference, ChunkingType, ChunkingTypeOption},
6+
module::Module,
7+
reference::ModuleReference,
68
resolve::ModuleResolveResult,
79
};
810

@@ -42,4 +44,12 @@ impl ModuleReference for NextServerComponentModuleReference {
4244
}
4345

4446
#[turbo_tasks::value_impl]
45-
impl ChunkableModuleReference for NextServerComponentModuleReference {}
47+
impl ChunkableModuleReference for NextServerComponentModuleReference {
48+
#[turbo_tasks::function]
49+
fn chunking_type(&self) -> Vc<ChunkingTypeOption> {
50+
Vc::cell(Some(ChunkingType::Shared {
51+
inherit_async: true,
52+
merge_tag: None,
53+
}))
54+
}
55+
}

crates/next-core/src/next_server_utility/server_utility_reference.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ use anyhow::Result;
22
use turbo_rcstr::RcStr;
33
use turbo_tasks::{ResolvedVc, ValueToString, Vc};
44
use turbopack_core::{
5-
chunk::ChunkableModuleReference, module::Module, reference::ModuleReference,
5+
chunk::{ChunkableModuleReference, ChunkingType, ChunkingTypeOption},
6+
module::Module,
7+
reference::ModuleReference,
68
resolve::ModuleResolveResult,
79
};
810

@@ -42,4 +44,12 @@ impl ModuleReference for NextServerUtilityModuleReference {
4244
}
4345

4446
#[turbo_tasks::value_impl]
45-
impl ChunkableModuleReference for NextServerUtilityModuleReference {}
47+
impl ChunkableModuleReference for NextServerUtilityModuleReference {
48+
#[turbo_tasks::function]
49+
fn chunking_type(&self) -> Vc<ChunkingTypeOption> {
50+
Vc::cell(Some(ChunkingType::Shared {
51+
inherit_async: true,
52+
merge_tag: Some("next-server-utility".into()),
53+
}))
54+
}
55+
}

turbopack/crates/turbopack-core/src/chunk/chunk_group.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,9 @@ pub async fn chunk_group_content(
242242
};
243243

244244
Ok(match edge {
245-
ChunkingType::Parallel | ChunkingType::ParallelInheritAsync => {
245+
ChunkingType::Parallel
246+
| ChunkingType::ParallelInheritAsync
247+
| ChunkingType::Shared { .. } => {
246248
if is_available {
247249
GraphTraversalAction::Skip
248250
} else {

turbopack/crates/turbopack-core/src/chunk/mod.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ pub enum ChunkingType {
168168
Parallel,
169169
/// Module is placed in the same chunk group and is loaded in parallel. It
170170
/// becomes an async module when the referenced module is async.
171+
// TODO make inherit_async a separate field
171172
ParallelInheritAsync,
172173
/// An async loader is placed into the referencing chunk and loads the
173174
/// separate chunk group in which the module is placed.
@@ -179,6 +180,13 @@ pub enum ChunkingType {
179180
_ty: ChunkGroupType,
180181
merge_tag: Option<RcStr>,
181182
},
183+
/// Create a new chunk group in a separate context, merging references with the same tag into a
184+
/// single chunk group. It provides available modules to the current chunk group. It's assumed
185+
/// to be loaded before the current chunk group.
186+
Shared {
187+
inherit_async: bool,
188+
merge_tag: Option<RcStr>,
189+
},
182190
// Module not placed in chunk group, but its references are still followed.
183191
Traced,
184192
}

turbopack/crates/turbopack-core/src/introspect/utils.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ fn isolated_reference_ty() -> Vc<RcStr> {
3838
Vc::cell("isolated reference".into())
3939
}
4040

41+
#[turbo_tasks::function]
42+
fn shared_reference_ty() -> Vc<RcStr> {
43+
Vc::cell("shared reference".into())
44+
}
45+
4146
#[turbo_tasks::function]
4247
fn traced_reference_ty() -> Vc<RcStr> {
4348
Vc::cell("traced reference".into())
@@ -82,6 +87,7 @@ pub async fn children_from_module_references(
8287
}
8388
Some(ChunkingType::Async) => key = async_reference_ty(),
8489
Some(ChunkingType::Isolated { .. }) => key = isolated_reference_ty(),
90+
Some(ChunkingType::Shared { .. }) => key = shared_reference_ty(),
8591
Some(ChunkingType::Traced) => key = traced_reference_ty(),
8692
}
8793
}

turbopack/crates/turbopack-core/src/module_graph/async_module_info.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,15 +88,23 @@ async fn compute_async_module_info_single(
8888

8989
// edges.push((parent_module, module, async_modules.contains(&module)));
9090
match chunking_type {
91-
ChunkingType::ParallelInheritAsync => {
91+
ChunkingType::ParallelInheritAsync
92+
| ChunkingType::Shared {
93+
inherit_async: true,
94+
..
95+
} => {
9296
if async_modules.contains(&module) {
9397
async_modules.insert(parent_module);
9498
}
9599
}
96100
ChunkingType::Parallel
97101
| ChunkingType::Async
98102
| ChunkingType::Isolated { .. }
99-
| ChunkingType::Traced => {
103+
| ChunkingType::Traced
104+
| ChunkingType::Shared {
105+
inherit_async: false,
106+
..
107+
} => {
100108
// Nothing to propagate
101109
}
102110
}

turbopack/crates/turbopack-core/src/module_graph/chunk_group_info.rs

Lines changed: 49 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -107,15 +107,25 @@ pub enum ChunkGroup {
107107
merge_tag: RcStr,
108108
entries: Vec<ResolvedVc<Box<dyn Module>>>,
109109
},
110+
/// a module with an incoming non-merging shared edge
111+
Shared(ResolvedVc<Box<dyn Module>>),
112+
/// a module with an incoming merging shared edge
113+
SharedMerged {
114+
parent: usize,
115+
merge_tag: RcStr,
116+
entries: Vec<ResolvedVc<Box<dyn Module>>>,
117+
},
110118
}
111119

112120
impl ChunkGroup {
113121
pub fn entries(&self) -> impl Iterator<Item = ResolvedVc<Box<dyn Module>>> + '_ {
114122
match self {
115-
ChunkGroup::Entry(e) | ChunkGroup::Async(e) | ChunkGroup::Isolated(e) => {
116-
Either::Left(std::iter::once(*e))
117-
}
118-
ChunkGroup::IsolatedMerged { entries, .. } => Either::Right(entries.iter().copied()),
123+
ChunkGroup::Entry(e)
124+
| ChunkGroup::Async(e)
125+
| ChunkGroup::Isolated(e)
126+
| ChunkGroup::Shared(e) => Either::Left(std::iter::once(*e)),
127+
ChunkGroup::IsolatedMerged { entries, .. }
128+
| ChunkGroup::SharedMerged { entries, .. } => Either::Right(entries.iter().copied()),
119129
}
120130
}
121131
}
@@ -133,6 +143,13 @@ enum ChunkGroupKey {
133143
parent: ChunkGroupId,
134144
merge_tag: RcStr,
135145
},
146+
/// a module with an incoming merging shared edge
147+
Shared(ResolvedVc<Box<dyn Module>>),
148+
/// a module with an incoming merging shared edge
149+
SharedMerged {
150+
parent: ChunkGroupId,
151+
merge_tag: RcStr,
152+
},
136153
}
137154

138155
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
@@ -253,6 +270,11 @@ pub async fn compute_chunk_group_info(graph: &ModuleGraph) -> Result<Vc<ChunkGro
253270
} => ChunkGroupInheritance::ChunkGroup(Either::Left(std::iter::once(
254271
ChunkGroupKey::Isolated(node.module),
255272
))),
273+
ChunkingType::Shared {
274+
merge_tag: None, ..
275+
} => ChunkGroupInheritance::ChunkGroup(Either::Left(std::iter::once(
276+
ChunkGroupKey::Shared(node.module),
277+
))),
256278
ChunkingType::Isolated {
257279
merge_tag: Some(merge_tag),
258280
..
@@ -263,7 +285,23 @@ pub async fn compute_chunk_group_info(graph: &ModuleGraph) -> Result<Vc<ChunkGro
263285
parent: ChunkGroupId(parent),
264286
merge_tag: merge_tag.clone(),
265287
});
266-
ChunkGroupInheritance::ChunkGroup(Either::Right(chunk_groups))
288+
ChunkGroupInheritance::ChunkGroup(Either::Right(Either::Left(
289+
chunk_groups,
290+
)))
291+
}
292+
ChunkingType::Shared {
293+
merge_tag: Some(merge_tag),
294+
..
295+
} => {
296+
let parents = module_chunk_groups.get(&parent.module).unwrap();
297+
let chunk_groups =
298+
parents.iter().map(|parent| ChunkGroupKey::SharedMerged {
299+
parent: ChunkGroupId(parent),
300+
merge_tag: merge_tag.clone(),
301+
});
302+
ChunkGroupInheritance::ChunkGroup(Either::Right(Either::Right(
303+
chunk_groups,
304+
)))
267305
}
268306
ChunkingType::Traced => {
269307
// Traced modules are not placed in chunk groups
@@ -422,6 +460,12 @@ pub async fn compute_chunk_group_info(graph: &ModuleGraph) -> Result<Vc<ChunkGro
422460
entries: isolated_merged_entries.into_iter().collect(),
423461
}
424462
}
463+
ChunkGroupKey::Shared(module) => ChunkGroup::Shared(module),
464+
ChunkGroupKey::SharedMerged { parent, merge_tag } => ChunkGroup::SharedMerged {
465+
parent: parent.0 as usize,
466+
merge_tag,
467+
entries: isolated_merged_entries.into_iter().collect(),
468+
},
425469
})
426470
.collect(),
427471
}

0 commit comments

Comments
 (0)