Commit f68fc84e authored by Matt Mastracci's avatar Matt Mastracci

Inert/sync

parent 9ff61b82
...@@ -2,13 +2,13 @@ use std::{collections::hash_map::Entry, fmt::Display, future::Future, sync::Arc} ...@@ -2,13 +2,13 @@ use std::{collections::hash_map::Entry, fmt::Display, future::Future, sync::Arc}
use anyhow::Result; use anyhow::Result;
use parking_lot::Mutex; use parking_lot::Mutex;
use rustc_hash::{FxHashMap, FxHashSet}; use rustc_hash::{FxBuildHasher, FxHashMap, FxHashSet};
use swc_core::ecma::ast::Id; use swc_core::ecma::ast::Id;
use super::{JsValue, graph::VarGraph}; use super::{JsValue, graph::VarGraph};
use crate::analyzer::graph::VarMeta; use crate::analyzer::graph::VarMeta;
pub async fn link<'a, B, RB, F, RF>( pub async fn link<'a, B, F, RF>(
graph: &VarGraph, graph: &VarGraph,
mut val: JsValue, mut val: JsValue,
early_visitor: &B, early_visitor: &B,
...@@ -17,11 +17,17 @@ pub async fn link<'a, B, RB, F, RF>( ...@@ -17,11 +17,17 @@ pub async fn link<'a, B, RB, F, RF>(
var_cache: &Mutex<FxHashMap<Id, JsValue>>, var_cache: &Mutex<FxHashMap<Id, JsValue>>,
) -> Result<(JsValue, u32)> ) -> Result<(JsValue, u32)>
where where
RB: 'a + Future<Output = Result<(JsValue, bool)>> + Send, B: Fn(&mut JsValue) -> bool + Sync,
B: 'a + Fn(JsValue) -> RB + Sync,
RF: 'a + Future<Output = Result<(JsValue, bool)>> + Send, RF: 'a + Future<Output = Result<(JsValue, bool)>> + Send,
F: 'a + Fn(JsValue) -> RF + Sync, F: 'a + Fn(JsValue) -> RF + Sync,
{ {
// Fast path: values that are already fully resolved and will pass through
// both visitors unchanged. These have no children, no placeholders, and
// the visitor catch-all is a no-op for them.
if val.is_inert() {
return Ok((val, 0));
}
val.normalize(); val.normalize();
let (val, steps) = link_internal_iterative( let (val, steps) = link_internal_iterative(
graph, graph,
...@@ -75,7 +81,7 @@ impl Display for Step { ...@@ -75,7 +81,7 @@ impl Display for Step {
} }
// If a variable was already visited in this linking call, don't visit it again. // If a variable was already visited in this linking call, don't visit it again.
pub(crate) async fn link_internal_iterative<'a, B, RB, F, RF>( pub(crate) async fn link_internal_iterative<'a, B, F, RF>(
graph: &'a VarGraph, graph: &'a VarGraph,
val: JsValue, val: JsValue,
early_visitor: &'a B, early_visitor: &'a B,
...@@ -84,22 +90,21 @@ pub(crate) async fn link_internal_iterative<'a, B, RB, F, RF>( ...@@ -84,22 +90,21 @@ pub(crate) async fn link_internal_iterative<'a, B, RB, F, RF>(
var_cache: &Mutex<FxHashMap<Id, JsValue>>, var_cache: &Mutex<FxHashMap<Id, JsValue>>,
) -> Result<(JsValue, u32)> ) -> Result<(JsValue, u32)>
where where
RB: 'a + Future<Output = Result<(JsValue, bool)>> + Send, B: Fn(&mut JsValue) -> bool + Sync,
B: 'a + Fn(JsValue) -> RB + Sync,
RF: 'a + Future<Output = Result<(JsValue, bool)>> + Send, RF: 'a + Future<Output = Result<(JsValue, bool)>> + Send,
F: 'a + Fn(JsValue) -> RF + Sync, F: 'a + Fn(JsValue) -> RF + Sync,
{ {
let initial_capacity = (val.total_nodes() as usize).min(64); // Tracks the number of nodes in the queue and done combined
let mut total_nodes = val.total_nodes();
let initial_capacity = (total_nodes as usize).min(64);
let mut work_queue_stack: Vec<Step> = Vec::with_capacity(initial_capacity * 2); let mut work_queue_stack: Vec<Step> = Vec::with_capacity(initial_capacity * 2);
let mut done: Vec<JsValue> = Vec::with_capacity(initial_capacity); let mut done: Vec<JsValue> = Vec::with_capacity(initial_capacity);
// Tracks the number of nodes in the queue and done combined let mut cycle_stack: FxHashSet<Id> = FxHashSet::with_capacity_and_hasher(initial_capacity, FxBuildHasher::default());
let mut total_nodes = 0;
let mut cycle_stack: FxHashSet<Id> = FxHashSet::default();
let mut fun_args_depth: u32 = 0; let mut fun_args_depth: u32 = 0;
// Tracks the number linking steps so far // Tracks the number linking steps so far
let mut steps = 0; let mut steps = 0;
total_nodes += val.total_nodes();
work_queue_stack.push(Step::Enter(val)); work_queue_stack.push(Step::Enter(val));
while let Some(step) = work_queue_stack.pop() { while let Some(step) = work_queue_stack.pop() {
...@@ -269,7 +274,7 @@ where ...@@ -269,7 +274,7 @@ where
continue; continue;
} }
let (mut val, visit_modified) = early_visitor(val).await?; let visit_modified = early_visitor(&mut val);
val.debug_assert_total_nodes_up_to_date(); val.debug_assert_total_nodes_up_to_date();
if visit_modified && val.total_nodes() > LIMIT_NODE_SIZE { if visit_modified && val.total_nodes() > LIMIT_NODE_SIZE {
total_nodes += 1; total_nodes += 1;
......
...@@ -1145,6 +1145,16 @@ impl JsValue { ...@@ -1145,6 +1145,16 @@ impl JsValue {
// Methods regarding node count // Methods regarding node count
impl JsValue { impl JsValue {
/// Returns true if this value is fully resolved and will not be transformed
/// by the linker's visitors. These are leaf values with no placeholders and
/// no operations — the visitors' catch-all paths are no-ops for them.
pub fn is_inert(&self) -> bool {
matches!(
self,
JsValue::Constant(_) | JsValue::Url(_, _) | JsValue::Unknown { .. }
)
}
pub fn has_children(&self) -> bool { pub fn has_children(&self) -> bool {
self.total_nodes() > 1 self.total_nodes() > 1
} }
...@@ -3804,9 +3814,8 @@ pub mod test_utils { ...@@ -3804,9 +3814,8 @@ pub mod test_utils {
utils::module_value_to_well_known_object, utils::module_value_to_well_known_object,
}; };
pub async fn early_visitor(mut v: JsValue) -> Result<(JsValue, bool)> { pub fn early_visitor(v: &mut JsValue) -> bool {
let m = early_replace_builtin(&mut v); early_replace_builtin(v)
Ok((v, m))
} }
/// Visitor that replaces well known functions and objects with their /// Visitor that replaces well known functions and objects with their
......
...@@ -490,7 +490,7 @@ impl AnalysisState<'_> { ...@@ -490,7 +490,7 @@ impl AnalysisState<'_> {
Ok(link( Ok(link(
self.var_graph, self.var_graph,
value, value,
&early_value_visitor, &|v: &mut JsValue| early_replace_builtin(v),
&|value| { &|value| {
value_visitor( value_visitor(
*self.origin, *self.origin,
...@@ -3367,11 +3367,6 @@ fn require_resolve(path: FileSystemPath) -> String { ...@@ -3367,11 +3367,6 @@ fn require_resolve(path: FileSystemPath) -> String {
format!("/ROOT/{}", path.path.as_str()) format!("/ROOT/{}", path.path.as_str())
} }
async fn early_value_visitor(mut v: JsValue) -> Result<(JsValue, bool)> {
let modified = early_replace_builtin(&mut v);
Ok((v, modified))
}
async fn value_visitor( async fn value_visitor(
origin: Vc<Box<dyn ResolveOrigin>>, origin: Vc<Box<dyn ResolveOrigin>>,
v: JsValue, v: JsValue,
......
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