Unverified Commit 1c677a23 authored by Niklas Mischkulnig's avatar Niklas Mischkulnig Committed by GitHub

Turbopack: prevent ParseResult recomputation (#81244)

There was a mistake here: `process_parse_result` consumes the `ParseResult` via `ReadRef::try_unwrap`, but then inside of that, `EsmExports ::code_gen` does another read of the same cell with `parse_result.await?`. That required a recomputation because the cell was destroyed shortly before.
Don't do that anymore.

Now, this is the only remaining recomputation, but that is somewhat expected (multiple facade modules have the same empty `ParseResult`):

![Bildschirmfoto 2025-07-03 um 16 18 15](https://github.com/user-attachments/assets/468e1ef6-3e5a-4508-be48-da037bd44263)
parent d888428d
...@@ -870,9 +870,11 @@ impl EcmascriptModuleContentOptions { ...@@ -870,9 +870,11 @@ impl EcmascriptModuleContentOptions {
async fn merged_code_gens( async fn merged_code_gens(
&self, &self,
scope_hoisting_context: ScopeHoistingContext<'_>, scope_hoisting_context: ScopeHoistingContext<'_>,
eval_context: &EvalContext,
) -> Result<Vec<CodeGeneration>> { ) -> Result<Vec<CodeGeneration>> {
// Don't read `parsed` here again, it will cause a recomputation as `process_parse_result`
// has consumed the cell already.
let EcmascriptModuleContentOptions { let EcmascriptModuleContentOptions {
parsed,
module, module,
chunking_context, chunking_context,
references, references,
...@@ -906,7 +908,7 @@ impl EcmascriptModuleContentOptions { ...@@ -906,7 +908,7 @@ impl EcmascriptModuleContentOptions {
.code_generation( .code_generation(
**chunking_context, **chunking_context,
scope_hoisting_context, scope_hoisting_context,
Some(**parsed), eval_context,
*module, *module,
) )
.await?, .await?,
...@@ -1096,7 +1098,6 @@ impl EcmascriptModuleContent { ...@@ -1096,7 +1098,6 @@ impl EcmascriptModuleContent {
modules_header_width, modules_header_width,
comments, comments,
}, },
export_contexts: None,
is_esm: true, is_esm: true,
strict: true, strict: true,
original_source_map: CodeGenResultOriginalSourceMap::ScopeHoisting( original_source_map: CodeGenResultOriginalSourceMap::ScopeHoisting(
...@@ -1274,8 +1275,9 @@ async fn merge_modules( ...@@ -1274,8 +1275,9 @@ async fn merge_modules(
Ok(( Ok((
*module, *module,
content content
.export_contexts .scope_hoisting_syntax_contexts
.as_ref() .as_ref()
.map(|(_, export_contexts)| export_contexts)
.context("expected exports contexts")?, .context("expected exports contexts")?,
)) ))
}) })
...@@ -1294,7 +1296,7 @@ async fn merge_modules( ...@@ -1294,7 +1296,7 @@ async fn merge_modules(
program: &mut Program| { program: &mut Program| {
let _ = tracing::trace_span!("prepare module").entered(); let _ = tracing::trace_span!("prepare module").entered();
if let CodeGenResult { if let CodeGenResult {
scope_hoisting_syntax_contexts: Some(module_contexts), scope_hoisting_syntax_contexts: Some((module_contexts, _)),
.. ..
} = content } = content
{ {
...@@ -1555,14 +1557,16 @@ struct CodeGenResult { ...@@ -1555,14 +1557,16 @@ struct CodeGenResult {
program: Program, program: Program,
source_map: CodeGenResultSourceMap, source_map: CodeGenResultSourceMap,
comments: CodeGenResultComments, comments: CodeGenResultComments,
/// `eval_context.imports.exports`
export_contexts: Option<FxHashMap<RcStr, Id>>,
is_esm: bool, is_esm: bool,
strict: bool, strict: bool,
original_source_map: CodeGenResultOriginalSourceMap, original_source_map: CodeGenResultOriginalSourceMap,
minify: MinifyType, minify: MinifyType,
scope_hoisting_syntax_contexts: #[allow(clippy::type_complexity)]
Option<FxDashMap<ResolvedVc<Box<dyn EcmascriptChunkPlaceable + 'static>>, SyntaxContext>>, /// (Map<Module, corresponding context for imports>, `eval_context.imports.exports`)
scope_hoisting_syntax_contexts: Option<(
FxDashMap<ResolvedVc<Box<dyn EcmascriptChunkPlaceable + 'static>>, SyntaxContext>,
FxHashMap<RcStr, Id>,
)>,
} }
struct ScopeHoistingOptions<'a> { struct ScopeHoistingOptions<'a> {
...@@ -1584,14 +1588,14 @@ async fn process_parse_result( ...@@ -1584,14 +1588,14 @@ async fn process_parse_result(
with_consumed_parse_result( with_consumed_parse_result(
parsed, parsed,
async |mut program, source_map, globals, eval_context, comments| -> Result<CodeGenResult> { async |mut program, source_map, globals, eval_context, comments| -> Result<CodeGenResult> {
let (top_level_mark, is_esm, strict, export_contexts) = eval_context let (top_level_mark, is_esm, strict) = eval_context
.as_ref()
.map_either( .map_either(
|e| { |e| {
( (
e.top_level_mark, e.top_level_mark,
e.is_esm(specified_module_type), e.is_esm(specified_module_type),
e.imports.strict, e.imports.strict,
Cow::Owned(e.imports.exports),
) )
}, },
|e| { |e| {
...@@ -1599,7 +1603,6 @@ async fn process_parse_result( ...@@ -1599,7 +1603,6 @@ async fn process_parse_result(
e.top_level_mark, e.top_level_mark,
e.is_esm(specified_module_type), e.is_esm(specified_module_type),
e.imports.strict, e.imports.strict,
Cow::Borrowed(&e.imports.exports),
) )
}, },
) )
...@@ -1617,7 +1620,23 @@ async fn process_parse_result( ...@@ -1617,7 +1620,23 @@ async fn process_parse_result(
is_import_mark, is_import_mark,
globals, globals,
}; };
let code_gens = options.unwrap().merged_code_gens(ctx).await?; let code_gens = options
.unwrap()
.merged_code_gens(
ctx,
match &eval_context {
Either::Left(e) => e,
Either::Right(e) => e,
},
)
.await?;
let export_contexts = eval_context
.map_either(
|e| Cow::Owned(e.imports.exports),
|e| Cow::Borrowed(&e.imports.exports),
)
.into_inner();
let preserved_exports = let preserved_exports =
match &*scope_hoisting_options.module.get_exports().await? { match &*scope_hoisting_options.module.get_exports().await? {
EcmascriptExports::EsmExports(exports) => exports EcmascriptExports::EsmExports(exports) => exports
...@@ -1652,12 +1671,21 @@ async fn process_parse_result( ...@@ -1652,12 +1671,21 @@ async fn process_parse_result(
is_import_mark, is_import_mark,
module_syntax_contexts_cache, module_syntax_contexts_cache,
preserved_exports, preserved_exports,
export_contexts,
)), )),
prepend_ident_comment, prepend_ident_comment,
) )
} else if let Some(options) = options { } else if let Some(options) = options {
( (
options.merged_code_gens(ScopeHoistingContext::None).await?, options
.merged_code_gens(
ScopeHoistingContext::None,
match &eval_context {
Either::Left(e) => e,
Either::Right(e) => e,
},
)
.await?,
None, None,
None, None,
) )
...@@ -1696,7 +1724,7 @@ async fn process_parse_result( ...@@ -1696,7 +1724,7 @@ async fn process_parse_result(
} }
} }
if let Some((is_import_mark, _, preserved_exports)) = &retain_syntax_context { if let Some((is_import_mark, _, preserved_exports, _)) = &retain_syntax_context {
program.visit_mut_with(&mut hygiene_rename_only( program.visit_mut_with(&mut hygiene_rename_only(
Some(top_level_mark), Some(top_level_mark),
*is_import_mark, *is_import_mark,
...@@ -1733,13 +1761,13 @@ async fn process_parse_result( ...@@ -1733,13 +1761,13 @@ async fn process_parse_result(
comments, comments,
extra_comments, extra_comments,
}, },
// TODO ideally don't clone here at all
export_contexts: Some(export_contexts.into_owned()),
is_esm, is_esm,
strict, strict,
original_source_map: CodeGenResultOriginalSourceMap::Single(original_source_map), original_source_map: CodeGenResultOriginalSourceMap::Single(original_source_map),
minify, minify,
scope_hoisting_syntax_contexts: retain_syntax_context.map(|(_, ctxts, _)| ctxts), scope_hoisting_syntax_contexts: retain_syntax_context
// TODO ideally don't clone here
.map(|(_, ctxts, _, export_contexts)| (ctxts, export_contexts.into_owned())),
}) })
}, },
async |parse_result| -> Result<CodeGenResult> { async |parse_result| -> Result<CodeGenResult> {
...@@ -1769,7 +1797,6 @@ async fn process_parse_result( ...@@ -1769,7 +1797,6 @@ async fn process_parse_result(
}), }),
source_map: CodeGenResultSourceMap::None, source_map: CodeGenResultSourceMap::None,
comments: CodeGenResultComments::Empty, comments: CodeGenResultComments::Empty,
export_contexts: None,
is_esm: false, is_esm: false,
strict: false, strict: false,
original_source_map: CodeGenResultOriginalSourceMap::Single(None), original_source_map: CodeGenResultOriginalSourceMap::Single(None),
...@@ -1796,7 +1823,6 @@ async fn process_parse_result( ...@@ -1796,7 +1823,6 @@ async fn process_parse_result(
}), }),
source_map: CodeGenResultSourceMap::None, source_map: CodeGenResultSourceMap::None,
comments: CodeGenResultComments::Empty, comments: CodeGenResultComments::Empty,
export_contexts: None,
is_esm: false, is_esm: false,
strict: false, strict: false,
original_source_map: CodeGenResultOriginalSourceMap::Single(None), original_source_map: CodeGenResultOriginalSourceMap::Single(None),
...@@ -1891,7 +1917,6 @@ async fn emit_content( ...@@ -1891,7 +1917,6 @@ async fn emit_content(
strict, strict,
original_source_map, original_source_map,
minify, minify,
export_contexts: _,
scope_hoisting_syntax_contexts: _, scope_hoisting_syntax_contexts: _,
} = content; } = content;
......
...@@ -30,10 +30,10 @@ use turbopack_core::{ ...@@ -30,10 +30,10 @@ use turbopack_core::{
use super::base::ReferencedAsset; use super::base::ReferencedAsset;
use crate::{ use crate::{
EcmascriptModuleAsset, ScopeHoistingContext, EcmascriptModuleAsset, ScopeHoistingContext,
analyzer::graph::EvalContext,
chunk::{EcmascriptChunkPlaceable, EcmascriptExports}, chunk::{EcmascriptChunkPlaceable, EcmascriptExports},
code_gen::{CodeGeneration, CodeGenerationHoistedStmt}, code_gen::{CodeGeneration, CodeGenerationHoistedStmt},
magic_identifier, magic_identifier,
parse::ParseResult,
runtime_functions::{TURBOPACK_DYNAMIC, TURBOPACK_ESM}, runtime_functions::{TURBOPACK_DYNAMIC, TURBOPACK_ESM},
tree_shake::asset::EcmascriptModulePartAsset, tree_shake::asset::EcmascriptModulePartAsset,
utils::module_id_to_lit, utils::module_id_to_lit,
...@@ -555,7 +555,7 @@ impl EsmExports { ...@@ -555,7 +555,7 @@ impl EsmExports {
self: Vc<Self>, self: Vc<Self>,
chunking_context: Vc<Box<dyn ChunkingContext>>, chunking_context: Vc<Box<dyn ChunkingContext>>,
scope_hoisting_context: ScopeHoistingContext<'_>, scope_hoisting_context: ScopeHoistingContext<'_>,
parsed: Option<Vc<ParseResult>>, eval_context: &EvalContext,
module: ResolvedVc<Box<dyn EcmascriptChunkPlaceable>>, module: ResolvedVc<Box<dyn EcmascriptChunkPlaceable>>,
) -> Result<CodeGeneration> { ) -> Result<CodeGeneration> {
let export_usage_info = chunking_context.module_export_usage(*ResolvedVc::upcast(module)); let export_usage_info = chunking_context.module_export_usage(*ResolvedVc::upcast(module));
...@@ -570,12 +570,6 @@ impl EsmExports { ...@@ -570,12 +570,6 @@ impl EsmExports {
return Ok(CodeGeneration::empty()); return Ok(CodeGeneration::empty());
} }
let parsed = if let Some(parsed) = parsed {
Some(parsed.await?)
} else {
None
};
let mut dynamic_exports = Vec::<Box<Expr>>::new(); let mut dynamic_exports = Vec::<Box<Expr>>::new();
{ {
let id = if let Some(module) = scope_hoisting_context.module() let id = if let Some(module) = scope_hoisting_context.module()
...@@ -620,24 +614,16 @@ impl EsmExports { ...@@ -620,24 +614,16 @@ impl EsmExports {
// TODO ideally, this information would just be stored in // TODO ideally, this information would just be stored in
// EsmExport::LocalBinding and we wouldn't have to re-correlated this // EsmExport::LocalBinding and we wouldn't have to re-correlated this
// information with eval_context.imports.exports to get the syntax context. // information with eval_context.imports.exports to get the syntax context.
let binding = if let Some(parsed) = &parsed { let binding =
if let ParseResult::Ok { eval_context, .. } = &**parsed { if let Some((local, ctxt)) = eval_context.imports.exports.get(exported) {
if let Some((local, ctxt)) = eval_context.imports.exports.get(exported) Some((Cow::Borrowed(local.as_str()), *ctxt))
{
Some((Cow::Borrowed(local.as_str()), *ctxt))
} else {
bail!(
"Expected export to be in eval context {:?} {:?}",
exported,
eval_context.imports,
)
}
} else { } else {
None bail!(
} "Expected export to be in eval context {:?} {:?}",
} else { exported,
None eval_context.imports,
}; )
};
let (local, ctxt) = binding.unwrap_or_else(|| { let (local, ctxt) = binding.unwrap_or_else(|| {
// Fallback, shouldn't happen in practice // Fallback, shouldn't happen in practice
( (
......
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