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 {
......
use anyhow::Result; use anyhow::Result;
use bincode::{Decode, Encode}; use bincode::{Decode, Encode};
use rustc_hash::FxHashMap;
use swc_core::ecma::{ use swc_core::ecma::{
ast::{Expr, KeyValueProp, Prop, PropName, SimpleAssignTarget}, ast::{Expr, KeyValueProp, Prop, PropName, SimpleAssignTarget},
visit::fields::{CalleeField, PropField}, visit::fields::{CalleeField, PropField},
}; };
use turbo_rcstr::RcStr; use turbo_rcstr::RcStr;
use turbo_tasks::{NonLocalValue, ResolvedVc, Vc}; use turbo_tasks::{FxIndexMap, NonLocalValue, ResolvedVc, Vc};
use turbopack_core::chunk::ChunkingContext; use turbopack_core::chunk::ChunkingContext;
use crate::{ use crate::{
ScopeHoistingContext, ScopeHoistingContext,
ast_path_trie::{AstPathId, AstPathTrie}, ast_path_trie::{AstPathId, AstPathTrie},
code_gen::{CodeGen, CodeGeneration}, code_gen::CodeGeneration,
create_visitor, create_visitor,
references::esm::{ references::esm::{
EsmAssetReference, EsmAssetReference,
...@@ -19,61 +20,95 @@ use crate::{ ...@@ -19,61 +20,95 @@ use crate::{
}, },
}; };
#[derive(Hash, Clone, Debug, PartialEq, Eq, NonLocalValue, Encode, Decode)] #[derive(Clone, Debug, PartialEq, Eq, NonLocalValue, Encode, Decode, Hash)]
pub struct EsmBinding { struct EsmBinding {
reference: ResolvedVc<EsmAssetReference>,
export: Option<RcStr>, export: Option<RcStr>,
ast_path: AstPathId, ast_path: AstPathId,
keep_this: bool, keep_this: bool,
} }
impl EsmBinding { #[derive(Default, Clone, Debug)]
pub fn new( pub struct EsmBindingsBuilder {
bindings: FxIndexMap<ResolvedVc<EsmAssetReference>, Vec<EsmBinding>>,
}
#[derive(Default, Clone, Debug, PartialEq, Eq, NonLocalValue, Encode, Decode, Hash)]
pub struct EsmBindings {
#[allow(clippy::type_complexity)]
bindings: Box<[(ResolvedVc<EsmAssetReference>, Box<[EsmBinding]>)]>,
}
impl EsmBindingsBuilder {
pub fn add(
&mut self,
reference: ResolvedVc<EsmAssetReference>, reference: ResolvedVc<EsmAssetReference>,
export: Option<RcStr>, export: Option<RcStr>,
ast_path: AstPathId, ast_path: AstPathId,
) -> Self { ) {
EsmBinding { self.bindings
reference, .entry(reference)
.or_default()
.push(EsmBinding {
export, export,
ast_path, ast_path,
keep_this: false, keep_this: false,
} });
} }
/// Where possible, bind the namespace to `this` when the named import is called. /// Where possible, bind the namespace to `this` when the named import is called.
pub fn new_keep_this( pub fn add_keep_this(
&mut self,
reference: ResolvedVc<EsmAssetReference>, reference: ResolvedVc<EsmAssetReference>,
export: Option<RcStr>, export: Option<RcStr>,
ast_path: AstPathId, ast_path: AstPathId,
) -> Self { ) {
EsmBinding { self.bindings
reference, .entry(reference)
.or_default()
.push(EsmBinding {
export, export,
ast_path, ast_path,
keep_this: true, keep_this: true,
});
} }
pub fn build(self) -> Option<EsmBindings> {
if self.bindings.is_empty() {
return None;
}
Some(EsmBindings {
bindings: self
.bindings
.into_iter()
.map(|(k, v)| (k, v.into_boxed_slice()))
.collect(),
})
} }
}
impl EsmBindings {
pub async fn code_generation( pub async fn code_generation(
&self, &self,
trie: &AstPathTrie, trie: &AstPathTrie,
chunking_context: Vc<Box<dyn ChunkingContext>>, chunking_context: Vc<Box<dyn ChunkingContext>>,
scope_hoisting_context: ScopeHoistingContext<'_>, scope_hoisting_context: ScopeHoistingContext<'_>,
) -> Result<CodeGeneration> { ) -> Result<CodeGeneration> {
if chunking_context let mut visitors = vec![];
.unused_references()
.contains_key(&ResolvedVc::upcast(self.reference)) let unused_references = chunking_context.unused_references();
for (reference, bindings) in &self.bindings {
if unused_references
.contains_key(&ResolvedVc::upcast(*reference))
.await? .await?
{ {
return Ok(CodeGeneration::empty()); continue;
} }
let imported_module = reference.get_referenced_asset().await?;
let mut visitors = vec![]; let mut exports_cache: FxHashMap<Option<RcStr>, _> = FxHashMap::default();
#[derive(Clone)]
let export = self.export.clone();
let imported_module = self.reference.get_referenced_asset().await?;
enum ImportedIdent { enum ImportedIdent {
Module(ReferencedAssetIdent), Module(ReferencedAssetIdent),
None, None,
...@@ -81,7 +116,12 @@ impl EsmBinding { ...@@ -81,7 +116,12 @@ impl EsmBinding {
Empty, Empty,
Unresolvable, Unresolvable,
} }
for EsmBinding {
export,
ast_path,
keep_this,
} in bindings
{
let imported_ident = match &imported_module { let imported_ident = match &imported_module {
ReferencedAsset::None => ImportedIdent::None, ReferencedAsset::None => ImportedIdent::None,
ReferencedAsset::Empty => { ReferencedAsset::Empty => {
...@@ -93,19 +133,26 @@ impl EsmBinding { ...@@ -93,19 +133,26 @@ impl EsmBinding {
ImportedIdent::Empty ImportedIdent::Empty
} }
} }
imported_module => imported_module imported_module => match exports_cache.entry(export.clone()) {
.get_ident(chunking_context, export, scope_hoisting_context) std::collections::hash_map::Entry::Vacant(entry) => {
let ident: ImportedIdent = imported_module
.get_ident(chunking_context, export.clone(), scope_hoisting_context)
.await? .await?
.map_or(ImportedIdent::Unresolvable, ImportedIdent::Module), .map_or(ImportedIdent::Unresolvable, ImportedIdent::Module);
entry.insert(ident.clone());
ident
}
std::collections::hash_map::Entry::Occupied(entry) => entry.get().clone(),
},
}; };
// Walk up from the binding towards the root, stopping at the innermost node kind we // Walk up from the binding towards the root, stopping at the innermost node kind we
// know how to rewrite. // know how to rewrite.
let mut ast_path = self.ast_path; let mut ast_path = *ast_path;
loop { loop {
match trie.get(ast_path) { match trie.get(ast_path) {
// Shorthand properties get special treatment because we need to rewrite them to // Shorthand properties get special treatment because we need to rewrite
// normal key-value pairs. // them to normal key-value pairs.
Some(swc_core::ecma::visit::AstParentKind::Prop(PropField::Shorthand)) => { Some(swc_core::ecma::visit::AstParentKind::Prop(PropField::Shorthand)) => {
ast_path = trie.parent_or_root(ast_path); ast_path = trie.parent_or_root(ast_path);
visitors.push(create_visitor!( visitors.push(create_visitor!(
...@@ -149,7 +196,7 @@ impl EsmBinding { ...@@ -149,7 +196,7 @@ impl EsmBinding {
// Any other expression can be replaced with the import accessor. // Any other expression can be replaced with the import accessor.
Some(swc_core::ecma::visit::AstParentKind::Expr(_)) => { Some(swc_core::ecma::visit::AstParentKind::Expr(_)) => {
ast_path = trie.parent_or_root(ast_path); ast_path = trie.parent_or_root(ast_path);
let in_call = !self.keep_this let in_call = !keep_this
&& matches!( && matches!(
trie.get(ast_path), trie.get(ast_path),
Some(swc_core::ecma::visit::AstParentKind::Callee( Some(swc_core::ecma::visit::AstParentKind::Callee(
...@@ -222,13 +269,9 @@ impl EsmBinding { ...@@ -222,13 +269,9 @@ impl EsmBinding {
None => break, None => break,
} }
} }
Ok(CodeGeneration::visitors(visitors))
} }
} }
impl From<EsmBinding> for CodeGen { Ok(CodeGeneration::visitors(visitors))
fn from(val: EsmBinding) -> Self {
CodeGen::EsmBinding(val)
} }
} }
...@@ -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