Unverified Commit db03f83f authored by Niklas Mischkulnig's avatar Niklas Mischkulnig Committed by GitHub

Turbopack: batch EsmBinding codegen (#99722)

**View diff without whitespace**

Call these just once (per module), not once per referenced import
- `unused_references()`
- `reference.get_referenced_asset()`
- `imported_module.get_ident(export)`
parent a10d278e
...@@ -29,7 +29,7 @@ use crate::{ ...@@ -29,7 +29,7 @@ use crate::{
dynamic_expression::DynamicExpression, dynamic_expression::DynamicExpression,
emit_collect::CollectReferenceCodeGen, emit_collect::CollectReferenceCodeGen,
esm::{ esm::{
EsmBinding, EsmModuleItem, ImportMetaBinding, ImportMetaRef, EsmBindings, EsmModuleItem, ImportMetaBinding, ImportMetaRef,
dynamic::EsmAsyncAssetReferenceCodeGen, module_id::EsmModuleIdAssetReferenceCodeGen, dynamic::EsmAsyncAssetReferenceCodeGen, module_id::EsmModuleIdAssetReferenceCodeGen,
url::UrlAssetReferenceCodeGen, url::UrlAssetReferenceCodeGen,
}, },
...@@ -187,7 +187,7 @@ pub enum CodeGen { ...@@ -187,7 +187,7 @@ pub enum CodeGen {
ConstantConditionCodeGen(ConstantConditionCodeGen), ConstantConditionCodeGen(ConstantConditionCodeGen),
ConstantValueCodeGen(ConstantValueCodeGen), ConstantValueCodeGen(ConstantValueCodeGen),
DynamicExpression(DynamicExpression), DynamicExpression(DynamicExpression),
EsmBinding(EsmBinding), EsmBindings(EsmBindings),
EsmModuleItem(EsmModuleItem), EsmModuleItem(EsmModuleItem),
ExportsInfoBinding(ExportsInfoBinding), ExportsInfoBinding(ExportsInfoBinding),
ExportsInfoRef(ExportsInfoRef), ExportsInfoRef(ExportsInfoRef),
...@@ -225,7 +225,7 @@ impl CodeGen { ...@@ -225,7 +225,7 @@ impl CodeGen {
Self::CollectReferenceCodeGen(v) => v.code_generation(trie, ctx).await, Self::CollectReferenceCodeGen(v) => v.code_generation(trie, ctx).await,
Self::ConstantValueCodeGen(v) => v.code_generation(trie, ctx).await, Self::ConstantValueCodeGen(v) => v.code_generation(trie, ctx).await,
Self::DynamicExpression(v) => v.code_generation(trie, ctx).await, Self::DynamicExpression(v) => v.code_generation(trie, ctx).await,
Self::EsmBinding(v) => v.code_generation(trie, ctx, scope_hoisting_context).await, Self::EsmBindings(v) => v.code_generation(trie, ctx, scope_hoisting_context).await,
Self::EsmModuleItem(v) => v.code_generation(trie, ctx).await, Self::EsmModuleItem(v) => v.code_generation(trie, ctx).await,
Self::ExportsInfoBinding(v) => v.code_generation(trie, ctx, module, exports).await, Self::ExportsInfoBinding(v) => v.code_generation(trie, ctx, module, exports).await,
Self::ExportsInfoRef(v) => v.code_generation(trie, ctx).await, Self::ExportsInfoRef(v) => v.code_generation(trie, ctx).await,
......
...@@ -1139,7 +1139,7 @@ impl EcmascriptModuleContentOptions { ...@@ -1139,7 +1139,7 @@ impl EcmascriptModuleContentOptions {
export_registration_mode export_registration_mode
.unwrap_or_else(|| eval_context.imports.export_registration_mode()), .unwrap_or_else(|| eval_context.imports.export_registration_mode()),
export_registration_mode export_registration_mode
.map(|_| (&part_references[..], &esm_references_ref[..])), .map(|_| (part_references.as_ref(), esm_references_ref.as_ref())),
async_module_info.is_some(), async_module_info.is_some(),
) )
.await?; .await?;
......
...@@ -73,7 +73,7 @@ pub enum ReferencedAsset { ...@@ -73,7 +73,7 @@ pub enum ReferencedAsset {
Unresolvable, Unresolvable,
} }
#[derive(Debug)] #[derive(Clone, Debug)]
pub enum ReferencedAssetIdent { pub enum ReferencedAssetIdent {
/// The given export (or namespace) is a local binding in the current scope hoisting group. /// The given export (or namespace) is a local binding in the current scope hoisting group.
LocalBinding { LocalBinding {
...@@ -100,7 +100,7 @@ pub enum ReferencedAssetIdent { ...@@ -100,7 +100,7 @@ pub enum ReferencedAssetIdent {
} }
/// The source to import when initializing a `ReferencedAssetIdent::Module` variable. /// The source to import when initializing a `ReferencedAssetIdent::Module` variable.
#[derive(Debug)] #[derive(Clone, Debug)]
pub enum ImportSource { pub enum ImportSource {
/// Import an in-graph module. /// Import an in-graph module.
Module { Module {
......
...@@ -10,7 +10,7 @@ pub(crate) mod url; ...@@ -10,7 +10,7 @@ pub(crate) mod url;
pub use self::{ pub use self::{
base::{EsmAssetReference, EsmAssetReferenceOptions}, base::{EsmAssetReference, EsmAssetReferenceOptions},
binding::EsmBinding, binding::EsmBindings,
dynamic::EsmAsyncAssetReference, dynamic::EsmAsyncAssetReference,
export::{EsmExport, EsmExports, FoundExportType, Liveness}, export::{EsmExport, EsmExports, FoundExportType, Liveness},
mangle::generated_export_key, mangle::generated_export_key,
......
...@@ -131,9 +131,9 @@ use crate::{ ...@@ -131,9 +131,9 @@ use crate::{
dynamic_expression::DynamicExpression, dynamic_expression::DynamicExpression,
emit_collect::{CollectReference, EmitReference}, emit_collect::{CollectReference, EmitReference},
esm::{ esm::{
EsmAssetReference, EsmAssetReferenceOptions, EsmAsyncAssetReference, EsmBinding, EsmAssetReference, EsmAssetReferenceOptions, EsmAsyncAssetReference, ImportMetaBinding,
ImportMetaBinding, ImportMetaRef, UrlAssetReference, UrlRewriteBehavior, ImportMetaRef, UrlAssetReference, UrlRewriteBehavior, base::EsmAssetReferences,
base::EsmAssetReferences, module_id::EsmModuleIdAssetReference, binding::EsmBindingsBuilder, module_id::EsmModuleIdAssetReference,
}, },
exports::{EcmascriptExportsAnalysis, compute_ecmascript_module_exports}, exports::{EcmascriptExportsAnalysis, compute_ecmascript_module_exports},
exports_info::{ExportsInfoBinding, ExportsInfoRef}, exports_info::{ExportsInfoBinding, ExportsInfoRef},
...@@ -226,6 +226,8 @@ struct AnalyzeEcmascriptModuleResultBuilder { ...@@ -226,6 +226,8 @@ struct AnalyzeEcmascriptModuleResultBuilder {
// This caches repeated access because EsmAssetReference::new is not a turbo task function. // This caches repeated access because EsmAssetReference::new is not a turbo task function.
esm_references_rewritten: FxHashMap<usize, FxIndexMap<RcStr, ResolvedVc<EsmAssetReference>>>, esm_references_rewritten: FxHashMap<usize, FxIndexMap<RcStr, ResolvedVc<EsmAssetReference>>>,
esm_bindings: EsmBindingsBuilder,
code_gens: CodeGenCollection, code_gens: CodeGenCollection,
/// Interns the AST paths referenced by `code_gens`, so overlapping paths share storage. /// Interns the AST paths referenced by `code_gens`, so overlapping paths share storage.
/// Handed to the resulting [`CodeGens`] once analysis finishes. /// Handed to the resulting [`CodeGens`] once analysis finishes.
...@@ -253,6 +255,7 @@ impl AnalyzeEcmascriptModuleResultBuilder { ...@@ -253,6 +255,7 @@ impl AnalyzeEcmascriptModuleResultBuilder {
esm_reexport_references: Default::default(), esm_reexport_references: Default::default(),
esm_references_rewritten: Default::default(), esm_references_rewritten: Default::default(),
esm_references_free_var: Default::default(), esm_references_free_var: Default::default(),
esm_bindings: Default::default(),
code_gens: Default::default(), code_gens: Default::default(),
ast_paths: Default::default(), ast_paths: Default::default(),
async_module: ResolvedVc::cell(None), async_module: ResolvedVc::cell(None),
...@@ -459,6 +462,10 @@ impl AnalyzeEcmascriptModuleResultBuilder { ...@@ -459,6 +462,10 @@ impl AnalyzeEcmascriptModuleResultBuilder {
} }
} }
if let Some(esm_bindings) = std::mem::take(&mut self.esm_bindings).build() {
self.add_code_gen(CodeGen::EsmBindings(esm_bindings));
}
let references: Vec<_> = self.references.into_iter().collect(); let references: Vec<_> = self.references.into_iter().collect();
if !self.analyze_mode.is_codegen { if !self.analyze_mode.is_codegen {
...@@ -1473,11 +1480,11 @@ async fn analyze_ecmascript_module_internal( ...@@ -1473,11 +1480,11 @@ async fn analyze_ecmascript_module_internal(
.resolved_cell() .resolved_cell()
}, },
); );
analysis.add_code_gen(EsmBinding::new_keep_this( analysis.esm_bindings.add_keep_this(
named_reference, named_reference,
Some(export), Some(export),
analysis.intern_path(&ast_path), analysis.intern_path(&ast_path),
)); );
continue; continue;
} }
...@@ -1500,21 +1507,19 @@ async fn analyze_ecmascript_module_internal( ...@@ -1500,21 +1507,19 @@ async fn analyze_ecmascript_module_internal(
.resolved_cell() .resolved_cell()
}, },
); );
analysis.add_code_gen(EsmBinding::new( analysis.esm_bindings.add(
narrowed_reference, narrowed_reference,
export, export,
analysis.intern_path(&ast_path), analysis.intern_path(&ast_path),
)); );
continue; continue;
} }
} }
analysis.add_esm_reference(esm_reference_index); analysis.add_esm_reference(esm_reference_index);
analysis.add_code_gen(EsmBinding::new( analysis
*r, .esm_bindings
export, .add(*r, export, analysis.intern_path(&ast_path));
analysis.intern_path(&ast_path),
));
} }
} }
Effect::TypeOf { Effect::TypeOf {
...@@ -3868,11 +3873,11 @@ async fn handle_free_var_reference( ...@@ -3868,11 +3873,11 @@ async fn handle_free_var_reference(
}) })
.await?; .await?;
analysis.add_code_gen(EsmBinding::new( analysis.esm_bindings.add(
esm_reference, esm_reference,
export.clone(), export.clone(),
analysis.intern_path(ast_path), analysis.intern_path(ast_path),
)); );
} }
FreeVarReference::InputRelative(kind) => { FreeVarReference::InputRelative(kind) => {
let source_path = (*state.source).ident().await?.path.clone(); let source_path = (*state.source).ident().await?.path.clone();
......
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