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