Unverified Commit 1c341690 authored by Luke Sandberg's avatar Luke Sandberg Committed by GitHub

[turbopack] Lazily construct `resolved::Requests` for `EsmAssetReference` (#83662)

Right now we construct `Request` objects eagerly when analyzing modules.   This is on the critical path for analysis and can be expensive if there are a lot of exports (See [thread](https://vercel.slack.com/archives/C06PPGZ0FD3/p1757517287294729))

Instead just store the string and `parse` when resolving the reference.  This should have identical caching semantics and by deferring the work we may be able to avoid it altogether if certain references never get resolved due to `sideEffects` configuration.
parent 58d715e4
...@@ -43,10 +43,7 @@ use crate::{ ...@@ -43,10 +43,7 @@ use crate::{
code_gen::{CodeGeneration, CodeGenerationHoistedStmt}, code_gen::{CodeGeneration, CodeGenerationHoistedStmt},
export::Liveness, export::Liveness,
magic_identifier, magic_identifier,
references::{ references::{esm::EsmExport, util::throw_module_not_found_expr},
esm::EsmExport,
util::{request_to_string, throw_module_not_found_expr},
},
runtime_functions::{TURBOPACK_EXTERNAL_IMPORT, TURBOPACK_EXTERNAL_REQUIRE, TURBOPACK_IMPORT}, runtime_functions::{TURBOPACK_EXTERNAL_IMPORT, TURBOPACK_EXTERNAL_REQUIRE, TURBOPACK_IMPORT},
tree_shake::{TURBOPACK_PART_IMPORT_SOURCE, asset::EcmascriptModulePartAsset}, tree_shake::{TURBOPACK_PART_IMPORT_SOURCE, asset::EcmascriptModulePartAsset},
utils::module_id_to_lit, utils::module_id_to_lit,
...@@ -321,7 +318,8 @@ impl EsmAssetReferences { ...@@ -321,7 +318,8 @@ impl EsmAssetReferences {
#[derive(Hash, Debug)] #[derive(Hash, Debug)]
pub struct EsmAssetReference { pub struct EsmAssetReference {
pub origin: ResolvedVc<Box<dyn ResolveOrigin>>, pub origin: ResolvedVc<Box<dyn ResolveOrigin>>,
pub request: ResolvedVc<Request>, // Request is a string to avoid eagerly parsing into a `Request` VC
pub request: RcStr,
pub annotations: ImportAnnotations, pub annotations: ImportAnnotations,
pub issue_source: IssueSource, pub issue_source: IssueSource,
pub export_name: Option<ModulePart>, pub export_name: Option<ModulePart>,
...@@ -342,7 +340,7 @@ impl EsmAssetReference { ...@@ -342,7 +340,7 @@ impl EsmAssetReference {
impl EsmAssetReference { impl EsmAssetReference {
pub fn new( pub fn new(
origin: ResolvedVc<Box<dyn ResolveOrigin>>, origin: ResolvedVc<Box<dyn ResolveOrigin>>,
request: ResolvedVc<Request>, request: RcStr,
issue_source: IssueSource, issue_source: IssueSource,
annotations: ImportAnnotations, annotations: ImportAnnotations,
export_name: Option<ModulePart>, export_name: Option<ModulePart>,
...@@ -361,7 +359,7 @@ impl EsmAssetReference { ...@@ -361,7 +359,7 @@ impl EsmAssetReference {
pub fn new_pure( pub fn new_pure(
origin: ResolvedVc<Box<dyn ResolveOrigin>>, origin: ResolvedVc<Box<dyn ResolveOrigin>>,
request: ResolvedVc<Request>, request: RcStr,
issue_source: IssueSource, issue_source: IssueSource,
annotations: ImportAnnotations, annotations: ImportAnnotations,
export_name: Option<ModulePart>, export_name: Option<ModulePart>,
...@@ -424,8 +422,9 @@ impl ModuleReference for EsmAssetReference { ...@@ -424,8 +422,9 @@ impl ModuleReference for EsmAssetReference {
} }
} }
} }
let request = Request::parse(self.request.clone().into());
if let Request::Module { module, .. } = &*self.request.await? if let Request::Module { module, .. } = &*request.await?
&& module.is_match(TURBOPACK_PART_IMPORT_SOURCE) && module.is_match(TURBOPACK_PART_IMPORT_SOURCE)
{ {
if let Some(part) = &self.export_name { if let Some(part) = &self.export_name {
...@@ -445,7 +444,7 @@ impl ModuleReference for EsmAssetReference { ...@@ -445,7 +444,7 @@ impl ModuleReference for EsmAssetReference {
let result = esm_resolve( let result = esm_resolve(
self.get_origin().resolve().await?, self.get_origin().resolve().await?,
*self.request, request,
ty, ty,
false, false,
Some(self.issue_source), Some(self.issue_source),
...@@ -475,15 +474,8 @@ impl ModuleReference for EsmAssetReference { ...@@ -475,15 +474,8 @@ impl ModuleReference for EsmAssetReference {
#[turbo_tasks::value_impl] #[turbo_tasks::value_impl]
impl ValueToString for EsmAssetReference { impl ValueToString for EsmAssetReference {
#[turbo_tasks::function] #[turbo_tasks::function]
async fn to_string(&self) -> Result<Vc<RcStr>> { fn to_string(&self) -> Vc<RcStr> {
Ok(Vc::cell( Vc::cell(format!("import {} with {}", self.request, self.annotations).into())
format!(
"import {} with {}",
self.request.to_string().await?,
self.annotations
)
.into(),
))
} }
} }
...@@ -537,9 +529,9 @@ impl EsmAssetReference { ...@@ -537,9 +529,9 @@ impl EsmAssetReference {
ReferencedAsset::Unresolvable => { ReferencedAsset::Unresolvable => {
// Insert code that throws immediately at time of import if a request is // Insert code that throws immediately at time of import if a request is
// unresolvable // unresolvable
let request = request_to_string(*this.request).await?.to_string(); let request = &this.request;
let stmt = Stmt::Expr(ExprStmt { let stmt = Stmt::Expr(ExprStmt {
expr: Box::new(throw_module_not_found_expr(&request)), expr: Box::new(throw_module_not_found_expr(request)),
span: DUMMY_SP, span: DUMMY_SP,
}); });
return Ok(CodeGeneration::hoisted_stmt( return Ok(CodeGeneration::hoisted_stmt(
......
...@@ -738,42 +738,38 @@ pub async fn analyse_ecmascript_module_internal( ...@@ -738,42 +738,38 @@ pub async fn analyse_ecmascript_module_internal(
let mut should_add_evaluation = false; let mut should_add_evaluation = false;
let reference = EsmAssetReference::new( let reference = EsmAssetReference::new(
origin, origin,
Request::parse(RcStr::from(&*r.module_path).into()) RcStr::from(&*r.module_path),
.to_resolved()
.await?,
r.issue_source r.issue_source
.unwrap_or_else(|| IssueSource::from_source_only(source)), .unwrap_or_else(|| IssueSource::from_source_only(source)),
r.annotations.clone(), r.annotations.clone(),
match options.tree_shaking_mode { match &r.imported_symbol {
Some(TreeShakingMode::ModuleFragments) => match &r.imported_symbol { ImportedSymbol::ModuleEvaluation => {
ImportedSymbol::ModuleEvaluation => { should_add_evaluation = true;
should_add_evaluation = true; Some(ModulePart::evaluation())
Some(ModulePart::evaluation()) }
} ImportedSymbol::Symbol(name) => Some(ModulePart::export((&**name).into())),
ImportedSymbol::Symbol(name) => Some(ModulePart::export((&**name).into())), ImportedSymbol::PartEvaluation(part_id) | ImportedSymbol::Part(part_id) => {
ImportedSymbol::PartEvaluation(part_id) => { if !matches!(
should_add_evaluation = true; options.tree_shaking_mode,
Some(ModulePart::internal(*part_id)) Some(TreeShakingMode::ModuleFragments)
} ) {
ImportedSymbol::Part(part_id) => Some(ModulePart::internal(*part_id)),
ImportedSymbol::Exports => Some(ModulePart::exports()),
},
_ => match &r.imported_symbol {
ImportedSymbol::ModuleEvaluation => {
should_add_evaluation = true;
Some(ModulePart::evaluation())
}
ImportedSymbol::Symbol(name) => Some(ModulePart::export((&**name).into())),
ImportedSymbol::PartEvaluation(_) | ImportedSymbol::Part(_) => {
bail!( bail!(
"Internal imports doesn't exist in reexports only mode when \ "Internal imports only exist in reexports only mode when \
importing {:?} from {}", importing {:?} from {}",
r.imported_symbol, r.imported_symbol,
r.module_path r.module_path
); );
} }
ImportedSymbol::Exports => None, if matches!(&r.imported_symbol, ImportedSymbol::PartEvaluation(_)) {
}, should_add_evaluation = true;
}
Some(ModulePart::internal(*part_id))
}
ImportedSymbol::Exports => matches!(
options.tree_shaking_mode,
Some(TreeShakingMode::ModuleFragments)
)
.then(ModulePart::exports),
}, },
import_externals, import_externals,
) )
...@@ -1406,7 +1402,7 @@ pub async fn analyse_ecmascript_module_internal( ...@@ -1406,7 +1402,7 @@ pub async fn analyse_ecmascript_module_internal(
|| { || {
EsmAssetReference::new( EsmAssetReference::new(
original_reference.origin, original_reference.origin,
original_reference.request, original_reference.request.clone(),
original_reference.issue_source, original_reference.issue_source,
original_reference.annotations.clone(), original_reference.annotations.clone(),
Some(ModulePart::export(export.clone())), Some(ModulePart::export(export.clone())),
...@@ -2668,7 +2664,7 @@ async fn handle_free_var_reference( ...@@ -2668,7 +2664,7 @@ async fn handle_free_var_reference(
} else { } else {
state.origin state.origin
}, },
Request::parse(request.clone().into()).to_resolved().await?, request.clone(),
IssueSource::from_swc_offsets( IssueSource::from_swc_offsets(
state.source, state.source,
span.lo.to_u32(), span.lo.to_u32(),
......
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