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

[Turbopack] add ChunkingType::Shared (#76228)

### What?

use ChunkingType::Shared for server utils and components
parent 8f346de9
......@@ -2,7 +2,9 @@ use anyhow::Result;
use turbo_rcstr::RcStr;
use turbo_tasks::{ResolvedVc, ValueToString, Vc};
use turbopack_core::{
chunk::ChunkableModuleReference, module::Module, reference::ModuleReference,
chunk::{ChunkableModuleReference, ChunkingType, ChunkingTypeOption},
module::Module,
reference::ModuleReference,
resolve::ModuleResolveResult,
};
......@@ -42,4 +44,12 @@ impl ModuleReference for NextServerComponentModuleReference {
}
#[turbo_tasks::value_impl]
impl ChunkableModuleReference for NextServerComponentModuleReference {}
impl ChunkableModuleReference for NextServerComponentModuleReference {
#[turbo_tasks::function]
fn chunking_type(&self) -> Vc<ChunkingTypeOption> {
Vc::cell(Some(ChunkingType::Shared {
inherit_async: true,
merge_tag: None,
}))
}
}
......@@ -2,7 +2,9 @@ use anyhow::Result;
use turbo_rcstr::RcStr;
use turbo_tasks::{ResolvedVc, ValueToString, Vc};
use turbopack_core::{
chunk::ChunkableModuleReference, module::Module, reference::ModuleReference,
chunk::{ChunkableModuleReference, ChunkingType, ChunkingTypeOption},
module::Module,
reference::ModuleReference,
resolve::ModuleResolveResult,
};
......@@ -42,4 +44,12 @@ impl ModuleReference for NextServerUtilityModuleReference {
}
#[turbo_tasks::value_impl]
impl ChunkableModuleReference for NextServerUtilityModuleReference {}
impl ChunkableModuleReference for NextServerUtilityModuleReference {
#[turbo_tasks::function]
fn chunking_type(&self) -> Vc<ChunkingTypeOption> {
Vc::cell(Some(ChunkingType::Shared {
inherit_async: true,
merge_tag: Some("next-server-utility".into()),
}))
}
}
......@@ -242,7 +242,9 @@ pub async fn chunk_group_content(
};
Ok(match edge {
ChunkingType::Parallel | ChunkingType::ParallelInheritAsync => {
ChunkingType::Parallel
| ChunkingType::ParallelInheritAsync
| ChunkingType::Shared { .. } => {
if is_available {
GraphTraversalAction::Skip
} else {
......
......@@ -168,6 +168,7 @@ pub enum ChunkingType {
Parallel,
/// Module is placed in the same chunk group and is loaded in parallel. It
/// becomes an async module when the referenced module is async.
// TODO make inherit_async a separate field
ParallelInheritAsync,
/// An async loader is placed into the referencing chunk and loads the
/// separate chunk group in which the module is placed.
......@@ -179,6 +180,13 @@ pub enum ChunkingType {
_ty: ChunkGroupType,
merge_tag: Option<RcStr>,
},
/// Create a new chunk group in a separate context, merging references with the same tag into a
/// single chunk group. It provides available modules to the current chunk group. It's assumed
/// to be loaded before the current chunk group.
Shared {
inherit_async: bool,
merge_tag: Option<RcStr>,
},
// Module not placed in chunk group, but its references are still followed.
Traced,
}
......
......@@ -38,6 +38,11 @@ fn isolated_reference_ty() -> Vc<RcStr> {
Vc::cell("isolated reference".into())
}
#[turbo_tasks::function]
fn shared_reference_ty() -> Vc<RcStr> {
Vc::cell("shared reference".into())
}
#[turbo_tasks::function]
fn traced_reference_ty() -> Vc<RcStr> {
Vc::cell("traced reference".into())
......@@ -82,6 +87,7 @@ pub async fn children_from_module_references(
}
Some(ChunkingType::Async) => key = async_reference_ty(),
Some(ChunkingType::Isolated { .. }) => key = isolated_reference_ty(),
Some(ChunkingType::Shared { .. }) => key = shared_reference_ty(),
Some(ChunkingType::Traced) => key = traced_reference_ty(),
}
}
......
......@@ -88,7 +88,11 @@ async fn compute_async_module_info_single(
// edges.push((parent_module, module, async_modules.contains(&module)));
match chunking_type {
ChunkingType::ParallelInheritAsync => {
ChunkingType::ParallelInheritAsync
| ChunkingType::Shared {
inherit_async: true,
..
} => {
if async_modules.contains(&module) {
async_modules.insert(parent_module);
}
......@@ -96,7 +100,11 @@ async fn compute_async_module_info_single(
ChunkingType::Parallel
| ChunkingType::Async
| ChunkingType::Isolated { .. }
| ChunkingType::Traced => {
| ChunkingType::Traced
| ChunkingType::Shared {
inherit_async: false,
..
} => {
// Nothing to propagate
}
}
......
......@@ -107,15 +107,25 @@ pub enum ChunkGroup {
merge_tag: RcStr,
entries: Vec<ResolvedVc<Box<dyn Module>>>,
},
/// a module with an incoming non-merging shared edge
Shared(ResolvedVc<Box<dyn Module>>),
/// a module with an incoming merging shared edge
SharedMerged {
parent: usize,
merge_tag: RcStr,
entries: Vec<ResolvedVc<Box<dyn Module>>>,
},
}
impl ChunkGroup {
pub fn entries(&self) -> impl Iterator<Item = ResolvedVc<Box<dyn Module>>> + '_ {
match self {
ChunkGroup::Entry(e) | ChunkGroup::Async(e) | ChunkGroup::Isolated(e) => {
Either::Left(std::iter::once(*e))
}
ChunkGroup::IsolatedMerged { entries, .. } => Either::Right(entries.iter().copied()),
ChunkGroup::Entry(e)
| ChunkGroup::Async(e)
| ChunkGroup::Isolated(e)
| ChunkGroup::Shared(e) => Either::Left(std::iter::once(*e)),
ChunkGroup::IsolatedMerged { entries, .. }
| ChunkGroup::SharedMerged { entries, .. } => Either::Right(entries.iter().copied()),
}
}
}
......@@ -133,6 +143,13 @@ enum ChunkGroupKey {
parent: ChunkGroupId,
merge_tag: RcStr,
},
/// a module with an incoming merging shared edge
Shared(ResolvedVc<Box<dyn Module>>),
/// a module with an incoming merging shared edge
SharedMerged {
parent: ChunkGroupId,
merge_tag: RcStr,
},
}
#[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
} => ChunkGroupInheritance::ChunkGroup(Either::Left(std::iter::once(
ChunkGroupKey::Isolated(node.module),
))),
ChunkingType::Shared {
merge_tag: None, ..
} => ChunkGroupInheritance::ChunkGroup(Either::Left(std::iter::once(
ChunkGroupKey::Shared(node.module),
))),
ChunkingType::Isolated {
merge_tag: Some(merge_tag),
..
......@@ -263,7 +285,23 @@ pub async fn compute_chunk_group_info(graph: &ModuleGraph) -> Result<Vc<ChunkGro
parent: ChunkGroupId(parent),
merge_tag: merge_tag.clone(),
});
ChunkGroupInheritance::ChunkGroup(Either::Right(chunk_groups))
ChunkGroupInheritance::ChunkGroup(Either::Right(Either::Left(
chunk_groups,
)))
}
ChunkingType::Shared {
merge_tag: Some(merge_tag),
..
} => {
let parents = module_chunk_groups.get(&parent.module).unwrap();
let chunk_groups =
parents.iter().map(|parent| ChunkGroupKey::SharedMerged {
parent: ChunkGroupId(parent),
merge_tag: merge_tag.clone(),
});
ChunkGroupInheritance::ChunkGroup(Either::Right(Either::Right(
chunk_groups,
)))
}
ChunkingType::Traced => {
// Traced modules are not placed in chunk groups
......@@ -422,6 +460,12 @@ pub async fn compute_chunk_group_info(graph: &ModuleGraph) -> Result<Vc<ChunkGro
entries: isolated_merged_entries.into_iter().collect(),
}
}
ChunkGroupKey::Shared(module) => ChunkGroup::Shared(module),
ChunkGroupKey::SharedMerged { parent, merge_tag } => ChunkGroup::SharedMerged {
parent: parent.0 as usize,
merge_tag,
entries: isolated_merged_entries.into_iter().collect(),
},
})
.collect(),
}
......
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