Unverified Commit de17a9b1 authored by Donny/강동윤's avatar Donny/강동윤 Committed by GitHub

feat(turbopack): Implement skipping of side effect free fragments (#71340)

### What?

### Why?

### How?

Closes NEXT-
Fixes #
parent 45f3ec08
...@@ -313,6 +313,7 @@ async fn parse_actions(module: Vc<Box<dyn Module>>) -> Result<Vc<OptionActionMap ...@@ -313,6 +313,7 @@ async fn parse_actions(module: Vc<Box<dyn Module>>) -> Result<Vc<OptionActionMap
| ModulePart::Exports | ModulePart::Exports
| ModulePart::Facade | ModulePart::Facade
| ModulePart::Internal(..) | ModulePart::Internal(..)
| ModulePart::InternalEvaluation(..)
) { ) {
return Ok(OptionActionMap::none()); return Ok(OptionActionMap::none());
} }
......
...@@ -283,15 +283,19 @@ impl AssetIdent { ...@@ -283,15 +283,19 @@ impl AssetIdent {
5_u8.deterministic_hash(&mut hasher); 5_u8.deterministic_hash(&mut hasher);
id.deterministic_hash(&mut hasher); id.deterministic_hash(&mut hasher);
} }
ModulePart::Locals => { ModulePart::InternalEvaluation(id) => {
6_u8.deterministic_hash(&mut hasher); 6_u8.deterministic_hash(&mut hasher);
id.deterministic_hash(&mut hasher);
} }
ModulePart::Exports => { ModulePart::Locals => {
7_u8.deterministic_hash(&mut hasher); 7_u8.deterministic_hash(&mut hasher);
} }
ModulePart::Facade => { ModulePart::Exports => {
8_u8.deterministic_hash(&mut hasher); 8_u8.deterministic_hash(&mut hasher);
} }
ModulePart::Facade => {
9_u8.deterministic_hash(&mut hasher);
}
} }
has_hash = true; has_hash = true;
......
...@@ -2929,6 +2929,8 @@ pub enum ModulePart { ...@@ -2929,6 +2929,8 @@ pub enum ModulePart {
RenamedNamespace { export: Vc<RcStr> }, RenamedNamespace { export: Vc<RcStr> },
/// A pointer to a specific part. /// A pointer to a specific part.
Internal(u32), Internal(u32),
/// A pointer to a specific part, but with evaluation.
InternalEvaluation(u32),
/// The local declarations of a module. /// The local declarations of a module.
Locals, Locals,
/// The whole exports of a module. /// The whole exports of a module.
...@@ -2968,6 +2970,10 @@ impl ModulePart { ...@@ -2968,6 +2970,10 @@ impl ModulePart {
ModulePart::Internal(id).cell() ModulePart::Internal(id).cell()
} }
#[turbo_tasks::function] #[turbo_tasks::function]
pub fn internal_evaluation(id: u32) -> Vc<Self> {
ModulePart::InternalEvaluation(id).cell()
}
#[turbo_tasks::function]
pub fn locals() -> Vc<Self> { pub fn locals() -> Vc<Self> {
ModulePart::Locals.cell() ModulePart::Locals.cell()
} }
...@@ -2999,7 +3005,8 @@ impl ValueToString for ModulePart { ...@@ -2999,7 +3005,8 @@ impl ValueToString for ModulePart {
ModulePart::RenamedNamespace { export } => { ModulePart::RenamedNamespace { export } => {
format!("export * as {}", export.await?).into() format!("export * as {}", export.await?).into()
} }
ModulePart::Internal(id) => format!("internal part {}", id,).into(), ModulePart::Internal(id) => format!("internal part {}", id).into(),
ModulePart::InternalEvaluation(id) => format!("internal part {}", id).into(),
ModulePart::Locals => "locals".into(), ModulePart::Locals => "locals".into(),
ModulePart::Exports => "exports".into(), ModulePart::Exports => "exports".into(),
ModulePart::Facade => "facade".into(), ModulePart::Facade => "facade".into(),
......
...@@ -608,7 +608,7 @@ pub(crate) async fn analyse_ecmascript_module_internal( ...@@ -608,7 +608,7 @@ pub(crate) async fn analyse_ecmascript_module_internal(
ImportedSymbol::Symbol(name) => Some(ModulePart::export((&**name).into())), ImportedSymbol::Symbol(name) => Some(ModulePart::export((&**name).into())),
ImportedSymbol::PartEvaluation(part_id) => { ImportedSymbol::PartEvaluation(part_id) => {
evaluation_references.push(i); evaluation_references.push(i);
Some(ModulePart::internal(*part_id)) Some(ModulePart::internal_evaluation(*part_id))
} }
ImportedSymbol::Part(part_id) => Some(ModulePart::internal(*part_id)), ImportedSymbol::Part(part_id) => Some(ModulePart::internal(*part_id)),
ImportedSymbol::Exports => Some(ModulePart::exports()), ImportedSymbol::Exports => Some(ModulePart::exports()),
......
...@@ -80,7 +80,9 @@ impl ModuleReference for EcmascriptModulePartReference { ...@@ -80,7 +80,9 @@ impl ModuleReference for EcmascriptModulePartReference {
| ModulePart::RenamedNamespace { .. } => { | ModulePart::RenamedNamespace { .. } => {
Vc::upcast(EcmascriptModuleFacadeModule::new(*self.module, *part)) Vc::upcast(EcmascriptModuleFacadeModule::new(*self.module, *part))
} }
ModulePart::Export(..) | ModulePart::Internal(..) => { ModulePart::Export(..)
| ModulePart::Internal(..)
| ModulePart::InternalEvaluation(..) => {
bail!( bail!(
"Unexpected ModulePart {} for EcmascriptModulePartReference", "Unexpected ModulePart {} for EcmascriptModulePartReference",
part.to_string().await? part.to_string().await?
......
...@@ -166,7 +166,9 @@ impl Module for EcmascriptModulePartAsset { ...@@ -166,7 +166,9 @@ impl Module for EcmascriptModulePartAsset {
// This is an internal part that is not for evaluation, so we don't need to // This is an internal part that is not for evaluation, so we don't need to
// force-add it. // force-add it.
PartId::Internal(.., false) => return None, PartId::Internal(.., false) => return None,
PartId::Internal(part_id, true) => ModulePart::internal(*part_id), PartId::Internal(part_id, true) => {
ModulePart::internal_evaluation(*part_id)
}
PartId::Export(name) => ModulePart::export(name.clone()), PartId::Export(name) => ModulePart::export(name.clone()),
_ => unreachable!( _ => unreachable!(
"PartId other than Internal and Export should not be used here" "PartId other than Internal and Export should not be used here"
......
...@@ -368,7 +368,9 @@ async fn get_part_id(result: &SplitResult, part: Vc<ModulePart>) -> Result<u32> ...@@ -368,7 +368,9 @@ async fn get_part_id(result: &SplitResult, part: Vc<ModulePart>) -> Result<u32>
ModulePart::Evaluation => Key::ModuleEvaluation, ModulePart::Evaluation => Key::ModuleEvaluation,
ModulePart::Export(export) => Key::Export(export.await?.as_str().into()), ModulePart::Export(export) => Key::Export(export.await?.as_str().into()),
ModulePart::Exports => Key::Exports, ModulePart::Exports => Key::Exports,
ModulePart::Internal(part_id) => return Ok(*part_id), ModulePart::Internal(part_id) | ModulePart::InternalEvaluation(part_id) => {
return Ok(*part_id)
}
ModulePart::Locals ModulePart::Locals
| ModulePart::Facade | ModulePart::Facade
| ModulePart::RenamedExport { .. } | ModulePart::RenamedExport { .. }
......
(globalThis.TURBOPACK = globalThis.TURBOPACK || []).push([ (globalThis.TURBOPACK = globalThis.TURBOPACK || []).push([
"output/4c35f_tests_snapshot_basic-tree-shake_tree-shake-test-1_input_index_af8faa.js", "output/4c35f_tests_snapshot_basic-tree-shake_tree-shake-test-1_input_index_f95732.js",
{}, {},
{"otherChunks":["output/4c35f_tests_snapshot_basic-tree-shake_tree-shake-test-1_input_index_d79193.js"],"runtimeModuleIds":["[project]/turbopack/crates/turbopack-tests/tests/snapshot/basic-tree-shake/tree-shake-test-1/input/index.js [test] (ecmascript)"]} {"otherChunks":["output/4c35f_tests_snapshot_basic-tree-shake_tree-shake-test-1_input_index_d79193.js"],"runtimeModuleIds":["[project]/turbopack/crates/turbopack-tests/tests/snapshot/basic-tree-shake/tree-shake-test-1/input/index.js [test] (ecmascript)"]}
]); ]);
......
...@@ -142,7 +142,7 @@ async fn apply_module_type( ...@@ -142,7 +142,7 @@ async fn apply_module_type(
None None
}; };
if let Some((part, _)) = part_ref { if let Some((part, _)) = part_ref {
if let ModulePart::Evaluation = &*part { if let ModulePart::Evaluation | ModulePart::InternalEvaluation(..) = &*part {
// Skip the evaluation part if the module is marked as side effect free. // Skip the evaluation part if the module is marked as side effect free.
let side_effect_free_packages = let side_effect_free_packages =
module_asset_context.side_effect_free_packages(); module_asset_context.side_effect_free_packages();
......
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