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