Unverified Commit 94aea60d authored by Niklas Mischkulnig's avatar Niklas Mischkulnig Committed by GitHub

Turbopack: lazily evaluate `obj` for member replacements (#75453)

Don't even evaluate the left side of `obj.prop` if we know that we don't have anything in the replacement maps that matches  `*.prop` anyway.

This particular helps in huge files with complex code and also containing many member expressions

```
testing against 0293c96cf32

canary b6f8743e
13,75 GB 
TURBO_ENGINE_READ_ONLY=1 NEXT_TURBOPACK_TRACING= TURBOPACK=1 TURBOPACK_BUILD=  507.86s user 77.80s system 847% cpu 1:09.11 total
TURBO_ENGINE_READ_ONLY=1 NEXT_TURBOPACK_TRACING= TURBOPACK=1 TURBOPACK_BUILD=  503.13s user 77.86s system 855% cpu 1:07.90 total


mischnic/analyzer-faster-members ed38e94ba2
13,75 GB 
TURBO_ENGINE_READ_ONLY=1 NEXT_TURBOPACK_TRACING= TURBOPACK=1 TURBOPACK_BUILD=  499.54s user 79.28s system 857% cpu 1:07.51 total
TURBO_ENGINE_READ_ONLY=1 NEXT_TURBOPACK_TRACING= TURBOPACK=1 TURBOPACK_BUILD=  498.31s user 76.99s system 855% cpu 1:07.27 total
```
parent 674d32be
...@@ -242,10 +242,14 @@ impl From<CompileTimeDefineValue> for FreeVarReference { ...@@ -242,10 +242,14 @@ impl From<CompileTimeDefineValue> for FreeVarReference {
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct FreeVarReferences(pub FxIndexMap<Vec<DefineableNameSegment>, FreeVarReference>); pub struct FreeVarReferences(pub FxIndexMap<Vec<DefineableNameSegment>, FreeVarReference>);
/// A map from the last element (the member prop) to a map of the rest of the name to the value.
#[turbo_tasks::value(transparent)] #[turbo_tasks::value(transparent)]
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct FreeVarReferencesIndividual( pub struct FreeVarReferencesIndividual(
pub FxIndexMap<Vec<DefineableNameSegment>, ResolvedVc<FreeVarReference>>, pub FxIndexMap<
DefineableNameSegment,
FxIndexMap<Vec<DefineableNameSegment>, ResolvedVc<FreeVarReference>>,
>,
); );
#[turbo_tasks::value_impl] #[turbo_tasks::value_impl]
...@@ -257,12 +261,20 @@ impl FreeVarReferences { ...@@ -257,12 +261,20 @@ impl FreeVarReferences {
#[turbo_tasks::function] #[turbo_tasks::function]
pub fn individual(&self) -> Vc<FreeVarReferencesIndividual> { pub fn individual(&self) -> Vc<FreeVarReferencesIndividual> {
Vc::cell( let mut result: FxIndexMap<
self.0 DefineableNameSegment,
.iter() FxIndexMap<Vec<DefineableNameSegment>, ResolvedVc<FreeVarReference>>,
.map(|(key, value)| (key.clone(), value.clone().resolved_cell())) > = FxIndexMap::default();
.collect(),
) for (key, value) in &self.0 {
let (last_key, key) = key.split_last().unwrap();
result
.entry(last_key.clone())
.or_default()
.insert(key.to_vec(), value.clone().resolved_cell());
}
Vc::cell(result)
} }
} }
......
...@@ -1929,46 +1929,45 @@ impl JsValue { ...@@ -1929,46 +1929,45 @@ impl JsValue {
} }
} }
/// Returns any matching defined replacement that matches this value. /// Returns any matching defined replacement that matches this value (the replacement that
/// /// matches `$self.$prop`).
/// Optionally when passed a VarGraph, verifies that the first segment was not reassigned.
/// ///
/// Optionally also prefixes `self` with `prefix`, e.g. to be able to match `typeof foo` if /// Optionally when passed a VarGraph, verifies that the first segment is not a local
/// `self` is just `foo`. /// variable/was not reassigned.
pub fn match_free_var_reference<'a, T>( pub fn match_free_var_reference<'a, T>(
&self, &self,
var_graph: Option<&VarGraph>, var_graph: Option<&VarGraph>,
free_var_references: &'a FxIndexMap<Vec<DefineableNameSegment>, T>, free_var_references: &'a FxIndexMap<
prefix_self: &Option<DefineableNameSegment>, DefineableNameSegment,
FxIndexMap<Vec<DefineableNameSegment>, T>,
>,
prop: &DefineableNameSegment,
) -> Option<&'a T> { ) -> Option<&'a T> {
if let Some(def_name_len) = self.get_defineable_name_len() { if let Some(def_name_len) = self.get_defineable_name_len() {
for (name, value) in free_var_references.iter() { if let Some(references) = free_var_references.get(prop) {
if name.len() != def_name_len + (prefix_self.is_some() as usize) { for (name, value) in references {
continue; if name.len() != def_name_len {
}
let mut name_rev_it = name.iter().map(Cow::Borrowed).rev();
if let Some(prefix_self) = prefix_self {
if name_rev_it.next().unwrap().as_ref() != prefix_self {
continue; continue;
} }
}
if name_rev_it.eq(self.iter_defineable_name_rev()) { let name_rev_it = name.iter().map(Cow::Borrowed).rev();
if let Some(var_graph) = var_graph { if name_rev_it.eq(self.iter_defineable_name_rev()) {
if let DefineableNameSegment::Name(first_str) = name.first().unwrap() { if let Some(var_graph) = var_graph {
let first_str: &str = first_str; if let DefineableNameSegment::Name(first_str) = name.first().unwrap() {
if var_graph let first_str: &str = first_str;
.free_var_ids if var_graph
.get(&first_str.into()) .free_var_ids
.is_some_and(|id| var_graph.values.contains_key(id)) .get(&first_str.into())
{ .is_some_and(|id| var_graph.values.contains_key(id))
// `typeof foo...` but `foo` was reassigned {
return None; // `typeof foo...` but `foo` was reassigned
return None;
}
} }
} }
}
return Some(value); return Some(value);
}
} }
} }
} }
...@@ -1976,8 +1975,7 @@ impl JsValue { ...@@ -1976,8 +1975,7 @@ impl JsValue {
None None
} }
/// Returns any matching defined replacement that matches this value. Optionally also prefixes /// Returns any matching defined replacement that matches this value.
/// `self` with `prefix`, e.g. to be able to match `typeof foo` if `self` is just `foo`.
pub fn match_define<'a, T>( pub fn match_define<'a, T>(
&self, &self,
defines: &'a FxIndexMap<Vec<DefineableNameSegment>, T>, defines: &'a FxIndexMap<Vec<DefineableNameSegment>, T>,
......
...@@ -18,7 +18,7 @@ pub mod unreachable; ...@@ -18,7 +18,7 @@ pub mod unreachable;
pub mod util; pub mod util;
pub mod worker; pub mod worker;
use std::{borrow::Cow, collections::BTreeMap, mem::take, ops::Deref, sync::Arc}; use std::{borrow::Cow, collections::BTreeMap, future::Future, mem::take, ops::Deref, sync::Arc};
use anyhow::{bail, Result}; use anyhow::{bail, Result};
use constant_condition::{ConstantConditionCodeGen, ConstantConditionValue}; use constant_condition::{ConstantConditionCodeGen, ConstantConditionValue};
...@@ -49,11 +49,14 @@ use swc_core::{ ...@@ -49,11 +49,14 @@ use swc_core::{
}; };
use tracing::Instrument; use tracing::Instrument;
use turbo_rcstr::RcStr; use turbo_rcstr::RcStr;
use turbo_tasks::{FxIndexSet, ResolvedVc, TryJoinIterExt, Upcast, Value, ValueToString, Vc}; use turbo_tasks::{
FxIndexMap, FxIndexSet, ReadRef, ResolvedVc, TryJoinIterExt, Upcast, Value, ValueToString, Vc,
};
use turbo_tasks_fs::FileSystemPath; use turbo_tasks_fs::FileSystemPath;
use turbopack_core::{ use turbopack_core::{
compile_time_info::{ compile_time_info::{
CompileTimeInfo, DefineableNameSegment, FreeVarReference, FreeVarReferences, CompileTimeInfo, DefineableNameSegment, FreeVarReference, FreeVarReferences,
FreeVarReferencesIndividual,
}, },
environment::Rendering, environment::Rendering,
error::PrettyPrintError, error::PrettyPrintError,
...@@ -323,6 +326,7 @@ struct AnalysisState<'a> { ...@@ -323,6 +326,7 @@ struct AnalysisState<'a> {
import_externals: bool, import_externals: bool,
ignore_dynamic_requests: bool, ignore_dynamic_requests: bool,
url_rewrite_behavior: Option<UrlRewriteBehavior>, url_rewrite_behavior: Option<UrlRewriteBehavior>,
free_var_references: ReadRef<FreeVarReferencesIndividual>,
} }
impl AnalysisState<'_> { impl AnalysisState<'_> {
...@@ -337,6 +341,7 @@ impl AnalysisState<'_> { ...@@ -337,6 +341,7 @@ impl AnalysisState<'_> {
*self.origin, *self.origin,
value, value,
*self.compile_time_info, *self.compile_time_info,
&self.free_var_references,
self.var_graph, self.var_graph,
attributes, attributes,
) )
...@@ -906,6 +911,11 @@ pub(crate) async fn analyse_ecmascript_module_internal( ...@@ -906,6 +911,11 @@ pub(crate) async fn analyse_ecmascript_module_internal(
import_externals: options.import_externals, import_externals: options.import_externals,
ignore_dynamic_requests: options.ignore_dynamic_requests, ignore_dynamic_requests: options.ignore_dynamic_requests,
url_rewrite_behavior: options.url_rewrite_behavior, url_rewrite_behavior: options.url_rewrite_behavior,
free_var_references: compile_time_info
.await?
.free_var_references
.individual()
.await?,
}; };
enum Action { enum Action {
...@@ -1230,9 +1240,9 @@ pub(crate) async fn analyse_ecmascript_module_internal( ...@@ -1230,9 +1240,9 @@ pub(crate) async fn analyse_ecmascript_module_internal(
span, span,
in_try: _, in_try: _,
} => { } => {
let obj = analysis_state // Intentionally not awaited because `handle_member` reads this only when neeed.
.link_value(*obj, ImportAttributes::empty_ref()) let obj = analysis_state.link_value(*obj, ImportAttributes::empty_ref());
.await?;
let prop = analysis_state let prop = analysis_state
.link_value(*prop, ImportAttributes::empty_ref()) .link_value(*prop, ImportAttributes::empty_ref())
.await?; .await?;
...@@ -2270,42 +2280,57 @@ async fn handle_call<G: Fn(Vec<Effect>) + Send + Sync>( ...@@ -2270,42 +2280,57 @@ async fn handle_call<G: Fn(Vec<Effect>) + Send + Sync>(
async fn handle_member( async fn handle_member(
ast_path: &[AstParentKind], ast_path: &[AstParentKind],
obj: JsValue, link_obj: impl Future<Output = Result<JsValue>> + Send + Sync,
prop: JsValue, prop: JsValue,
span: Span, span: Span,
state: &AnalysisState<'_>, state: &AnalysisState<'_>,
analysis: &mut AnalyzeEcmascriptModuleResultBuilder, analysis: &mut AnalyzeEcmascriptModuleResultBuilder,
) -> Result<()> { ) -> Result<()> {
if let Some(prop) = prop.as_str() { if let Some(prop) = prop.as_str() {
let prop = DefineableNameSegment::Name(prop.into()); let prop_seg = DefineableNameSegment::Name(prop.into());
if let Some(def_name_len) = obj.get_defineable_name_len() {
let compile_time_info = state.compile_time_info.await?; let references = state.free_var_references.get(&prop_seg);
let free_var_references = compile_time_info.free_var_references.individual().await?; let is_prop_cache = prop == "cache";
for (name, value) in free_var_references.iter() {
if name.len() != def_name_len + 1 { // This isn't pretty, but we cannot await the future twice in the two branches below.
continue; let obj = if references.is_some() || is_prop_cache {
} Some(link_obj.await?)
let mut it = name.iter().map(Cow::Borrowed).rev(); } else {
if it.next().unwrap().as_ref() != &prop { None
continue; };
}
if it.eq(obj.iter_defineable_name_rev()) if let Some(references) = references {
&& handle_free_var_reference(ast_path, &*value.await?, span, state, analysis) let obj = obj.as_ref().unwrap();
if let Some(def_name_len) = obj.get_defineable_name_len() {
for (name, value) in references {
if name.len() != def_name_len {
continue;
}
let it = name.iter().map(Cow::Borrowed).rev();
if it.eq(obj.iter_defineable_name_rev())
&& handle_free_var_reference(
ast_path,
&*value.await?,
span,
state,
analysis,
)
.await? .await?
{ {
return Ok(()); return Ok(());
}
} }
} }
} }
}
match (obj, prop) { if is_prop_cache {
( if let JsValue::WellKnownFunction(WellKnownFunctionKind::Require { .. }) =
JsValue::WellKnownFunction(WellKnownFunctionKind::Require { .. }), obj.as_ref().unwrap()
JsValue::Constant(s), {
) if s.as_str() == Some("cache") => { analysis.add_code_gen(CjsRequireCacheAccess::new(ast_path.to_vec().into()));
analysis.add_code_gen(CjsRequireCacheAccess::new(ast_path.to_vec().into())); }
} }
_ => {}
} }
Ok(()) Ok(())
...@@ -2326,7 +2351,7 @@ async fn handle_typeof( ...@@ -2326,7 +2351,7 @@ async fn handle_typeof(
.free_var_references .free_var_references
.individual() .individual()
.await?, .await?,
&Some(DefineableNameSegment::TypeOf), &DefineableNameSegment::TypeOf,
) { ) {
handle_free_var_reference(ast_path, &*value.await?, span, state, analysis).await?; handle_free_var_reference(ast_path, &*value.await?, span, state, analysis).await?;
} }
...@@ -2342,19 +2367,19 @@ async fn handle_free_var( ...@@ -2342,19 +2367,19 @@ async fn handle_free_var(
analysis: &mut AnalyzeEcmascriptModuleResultBuilder, analysis: &mut AnalyzeEcmascriptModuleResultBuilder,
) -> Result<()> { ) -> Result<()> {
if let Some(def_name_len) = var.get_defineable_name_len() { if let Some(def_name_len) = var.get_defineable_name_len() {
let compile_time_info = state.compile_time_info.await?; let first = var.iter_defineable_name_rev().next().unwrap();
let free_var_references = compile_time_info.free_var_references.individual().await?; if let Some(references) = state.free_var_references.get(&*first) {
for (name, value) in free_var_references.iter() { for (name, value) in references {
if name.len() != def_name_len { if name.len() + 1 != def_name_len {
continue; continue;
} }
if var let it = name.iter().map(Cow::Borrowed).rev();
.iter_defineable_name_rev() if it.eq(var.iter_defineable_name_rev().skip(1)) {
.eq(name.iter().map(Cow::Borrowed).rev()) handle_free_var_reference(ast_path, &*value.await?, span, state, analysis)
{ .await?;
handle_free_var_reference(ast_path, &*value.await?, span, state, analysis).await?; return Ok(());
return Ok(()); }
} }
} }
} }
...@@ -2672,11 +2697,22 @@ async fn value_visitor( ...@@ -2672,11 +2697,22 @@ async fn value_visitor(
origin: Vc<Box<dyn ResolveOrigin>>, origin: Vc<Box<dyn ResolveOrigin>>,
v: JsValue, v: JsValue,
compile_time_info: Vc<CompileTimeInfo>, compile_time_info: Vc<CompileTimeInfo>,
free_var_references: &FxIndexMap<
DefineableNameSegment,
FxIndexMap<Vec<DefineableNameSegment>, ResolvedVc<FreeVarReference>>,
>,
var_graph: &VarGraph, var_graph: &VarGraph,
attributes: &ImportAttributes, attributes: &ImportAttributes,
) -> Result<(JsValue, bool)> { ) -> Result<(JsValue, bool)> {
let (mut v, modified) = let (mut v, modified) = value_visitor_inner(
value_visitor_inner(origin, v, compile_time_info, var_graph, attributes).await?; origin,
v,
compile_time_info,
free_var_references,
var_graph,
attributes,
)
.await?;
v.normalize_shallow(); v.normalize_shallow();
Ok((v, modified)) Ok((v, modified))
} }
...@@ -2685,6 +2721,10 @@ async fn value_visitor_inner( ...@@ -2685,6 +2721,10 @@ async fn value_visitor_inner(
origin: Vc<Box<dyn ResolveOrigin>>, origin: Vc<Box<dyn ResolveOrigin>>,
v: JsValue, v: JsValue,
compile_time_info: Vc<CompileTimeInfo>, compile_time_info: Vc<CompileTimeInfo>,
free_var_references: &FxIndexMap<
DefineableNameSegment,
FxIndexMap<Vec<DefineableNameSegment>, ResolvedVc<FreeVarReference>>,
>,
var_graph: &VarGraph, var_graph: &VarGraph,
attributes: &ImportAttributes, attributes: &ImportAttributes,
) -> Result<(JsValue, bool)> { ) -> Result<(JsValue, bool)> {
...@@ -2692,11 +2732,11 @@ async fn value_visitor_inner( ...@@ -2692,11 +2732,11 @@ async fn value_visitor_inner(
// This check is just an optimization // This check is just an optimization
if v.get_defineable_name_len().is_some() { if v.get_defineable_name_len().is_some() {
let compile_time_info = compile_time_info.await?; let compile_time_info = compile_time_info.await?;
if let JsValue::TypeOf(..) = v { if let JsValue::TypeOf(_, arg) = &v {
if let Some(value) = v.match_free_var_reference( if let Some(value) = arg.match_free_var_reference(
Some(var_graph), Some(var_graph),
&*compile_time_info.free_var_references.individual().await?, free_var_references,
&None, &DefineableNameSegment::TypeOf,
) { ) {
return Ok(((&*value.await?).into(), true)); return Ok(((&*value.await?).into(), true));
} }
......
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