Commit 49e83f5d authored by Matt Mastracci's avatar Matt Mastracci

Remove mutex checking (~1% bench improvement)

parent 1d14e93c
...@@ -39,7 +39,7 @@ const LIMIT_NODE_SIZE: u32 = 100; ...@@ -39,7 +39,7 @@ const LIMIT_NODE_SIZE: u32 = 100;
const LIMIT_IN_PROGRESS_NODES: u32 = 500; const LIMIT_IN_PROGRESS_NODES: u32 = 500;
const LIMIT_LINK_STEPS: u32 = 1500; const LIMIT_LINK_STEPS: u32 = 1500;
#[derive(Debug, Hash, Clone, Eq, PartialEq)] #[derive(Debug)]
enum Step { enum Step {
/// Take all chlidren out of the value (replacing temporarily with unknown) and queue them /// Take all chlidren out of the value (replacing temporarily with unknown) and queue them
/// for processing using individual `Enter`s. /// for processing using individual `Enter`s.
...@@ -89,11 +89,13 @@ where ...@@ -89,11 +89,13 @@ where
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 mut work_queue_stack: Vec<Step> = Vec::new(); let initial_capacity = (val.total_nodes() as usize).min(64);
let mut done: Vec<JsValue> = Vec::new(); let mut work_queue_stack: Vec<Step> = Vec::with_capacity(initial_capacity * 2);
let mut done: Vec<JsValue> = Vec::with_capacity(initial_capacity);
// Tracks the number of nodes in the queue and done combined // Tracks the number of nodes in the queue and done combined
let mut total_nodes = 0; let mut total_nodes = 0;
let mut cycle_stack: FxHashSet<Id> = FxHashSet::default(); let mut cycle_stack: FxHashSet<Id> = FxHashSet::default();
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;
...@@ -118,6 +120,7 @@ where ...@@ -118,6 +120,7 @@ where
} else { } else {
total_nodes -= 1; total_nodes -= 1;
let var_cache_lock = (cycle_stack.is_empty() let var_cache_lock = (cycle_stack.is_empty()
&& fun_args_depth == 0
&& fun_args_values.lock().is_empty()) && fun_args_values.lock().is_empty())
.then(|| var_cache.lock()); .then(|| var_cache.lock());
if let Some(val) = var_cache_lock.as_deref().and_then(|cache| cache.get(&var)) { if let Some(val) = var_cache_lock.as_deref().and_then(|cache| cache.get(&var)) {
...@@ -141,7 +144,10 @@ where ...@@ -141,7 +144,10 @@ where
// Leave a variable // Leave a variable
Step::LeaveVar(var) => { Step::LeaveVar(var) => {
cycle_stack.remove(&var); cycle_stack.remove(&var);
if cycle_stack.is_empty() && fun_args_values.lock().is_empty() { if cycle_stack.is_empty()
&& fun_args_depth == 0
&& fun_args_values.lock().is_empty()
{
var_cache.lock().insert(var, done.last().unwrap().clone()); var_cache.lock().insert(var, done.last().unwrap().clone());
} }
} }
...@@ -184,6 +190,7 @@ where ...@@ -184,6 +190,7 @@ where
total_nodes -= arg.total_nodes(); total_nodes -= arg.total_nodes();
} }
entry.insert(args); entry.insert(args);
fun_args_depth += 1;
work_queue_stack.push(Step::LeaveCall(func_ident)); work_queue_stack.push(Step::LeaveCall(func_ident));
work_queue_stack.push(Step::Enter(*return_value)); work_queue_stack.push(Step::Enter(*return_value));
} else { } else {
...@@ -206,6 +213,7 @@ where ...@@ -206,6 +213,7 @@ where
// - remove function arguments from the map // - remove function arguments from the map
Step::LeaveCall(func_ident) => { Step::LeaveCall(func_ident) => {
fun_args_values.lock().remove(&func_ident); fun_args_values.lock().remove(&func_ident);
fun_args_depth -= 1;
} }
// Enter a function // Enter a function
// We don't want to process the function return value yet, this will happen after // We don't want to process the function return value yet, this will happen after
...@@ -258,8 +266,9 @@ where ...@@ -258,8 +266,9 @@ where
true true
}); });
val.debug_assert_total_nodes_up_to_date(); val.debug_assert_total_nodes_up_to_date();
total_nodes -= val.total_nodes(); let count = val.total_nodes();
if val.total_nodes() > LIMIT_NODE_SIZE { total_nodes -= count;
if count > LIMIT_NODE_SIZE {
total_nodes += 1; total_nodes += 1;
done.push(JsValue::unknown_empty(true, "node limit reached")); done.push(JsValue::unknown_empty(true, "node limit reached"));
continue; continue;
...@@ -309,9 +318,10 @@ where ...@@ -309,9 +318,10 @@ where
}); });
val.debug_assert_total_nodes_up_to_date(); val.debug_assert_total_nodes_up_to_date();
total_nodes -= val.total_nodes(); let count = val.total_nodes();
total_nodes -= count;
if val.total_nodes() > LIMIT_NODE_SIZE { if count > LIMIT_NODE_SIZE {
total_nodes += 1; total_nodes += 1;
done.push(JsValue::unknown_empty(true, "node limit reached")); done.push(JsValue::unknown_empty(true, "node limit reached"));
continue; continue;
...@@ -332,9 +342,10 @@ where ...@@ -332,9 +342,10 @@ where
}); });
val.debug_assert_total_nodes_up_to_date(); val.debug_assert_total_nodes_up_to_date();
total_nodes -= val.total_nodes(); let count = val.total_nodes();
total_nodes -= count;
if val.total_nodes() > LIMIT_NODE_SIZE { if count > LIMIT_NODE_SIZE {
total_nodes += 1; total_nodes += 1;
done.push(JsValue::unknown_empty(true, "node limit reached")); done.push(JsValue::unknown_empty(true, "node limit reached"));
continue; continue;
...@@ -367,7 +378,10 @@ where ...@@ -367,7 +378,10 @@ where
match step { match step {
Step::LeaveVar(var) => { Step::LeaveVar(var) => {
cycle_stack.remove(&var); cycle_stack.remove(&var);
if cycle_stack.is_empty() && fun_args_values.lock().is_empty() { if cycle_stack.is_empty()
&& fun_args_depth == 0
&& fun_args_values.lock().is_empty()
{
var_cache.lock().insert( var_cache.lock().insert(
var, var,
JsValue::unknown_empty(true, "max number of linking steps reached"), JsValue::unknown_empty(true, "max number of linking steps reached"),
...@@ -376,6 +390,7 @@ where ...@@ -376,6 +390,7 @@ where
} }
Step::LeaveCall(func_ident) => { Step::LeaveCall(func_ident) => {
fun_args_values.lock().remove(&func_ident); fun_args_values.lock().remove(&func_ident);
fun_args_depth -= 1;
} }
_ => {} _ => {}
} }
......
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