Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 13 additions & 8 deletions crates/flow/src/incremental/invalidation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -342,11 +342,13 @@ impl InvalidationDetector {
fn tarjan_dfs(&self, v: &Path, state: &mut TarjanState, sccs: &mut Vec<Vec<PathBuf>>) {
// Initialize node
let index = state.index_counter;
state.indices.insert(v.to_path_buf(), index);
state.lowlinks.insert(v.to_path_buf(), index);
// ⚡ Bolt Optimization: Compute owned PathBuf once for insertion to avoid redundant O(E) allocations
Copy link

Copilot AI Apr 26, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment claims this avoids redundant "O(E) allocations", but this initialization block runs once per visited vertex (O(V)), and the clone() calls still allocate per owned PathBuf stored in each collection. Consider rewording to describe the actual win here (avoiding per-edge temporary PathBuf allocations during lookups), or just remove the complexity discussion from this line-level comment.

Suggested change
// ⚡ Bolt Optimization: Compute owned PathBuf once for insertion to avoid redundant O(E) allocations
// ⚡ Bolt Optimization: Materialize one owned `PathBuf` up front for the
// inserts/push below, while later lookups can continue to use borrowed `&Path`s.

Copilot uses AI. Check for mistakes.
let v_buf = v.to_path_buf();
state.indices.insert(v_buf.clone(), index);
state.lowlinks.insert(v_buf.clone(), index);
state.index_counter += 1;
state.stack.push(v.to_path_buf());
state.on_stack.insert(v.to_path_buf());
state.stack.push(v_buf.clone());
state.on_stack.insert(v_buf);

// Visit all successors (dependencies)
let dependencies = self.graph.get_dependencies(v);
Expand All @@ -358,19 +360,22 @@ impl InvalidationDetector {

// Update lowlink
let w_lowlink = *state.lowlinks.get(dep).unwrap();
let v_lowlink = state.lowlinks.get_mut(&v.to_path_buf()).unwrap();
// ⚡ Bolt Optimization: Use borrowed `&Path` reference `v` for map lookup instead of allocating
let v_lowlink = state.lowlinks.get_mut(v).unwrap();
*v_lowlink = (*v_lowlink).min(w_lowlink);
} else if state.on_stack.contains(dep) {
// Successor is on stack (part of current SCC)
let w_index = *state.indices.get(dep).unwrap();
let v_lowlink = state.lowlinks.get_mut(&v.to_path_buf()).unwrap();
// ⚡ Bolt Optimization: Use borrowed `&Path` reference `v` for map lookup instead of allocating
let v_lowlink = state.lowlinks.get_mut(v).unwrap();
*v_lowlink = (*v_lowlink).min(w_index);
}
}

// If v is a root node, pop the stack to create an SCC
let v_index = *state.indices.get(&v.to_path_buf()).unwrap();
let v_lowlink = *state.lowlinks.get(&v.to_path_buf()).unwrap();
// ⚡ Bolt Optimization: Use borrowed `&Path` reference `v` for map lookup instead of allocating
let v_index = *state.indices.get(v).unwrap();
let v_lowlink = *state.lowlinks.get(v).unwrap();

if v_lowlink == v_index {
let mut scc = Vec::new();
Expand Down
8 changes: 4 additions & 4 deletions crates/rule-engine/src/check_var.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,9 @@ fn get_vars_from_rules<'r>(rule: &'r Rule, utils: &'r RuleRegistration) -> Rapid
vars
}

fn check_var_in_constraints<'r>(
fn check_var_in_constraints(
mut vars: RapidSet<String>,
constraints: &'r RapidMap<thread_ast_engine::meta_var::MetaVariableID, Rule>,
constraints: &RapidMap<thread_ast_engine::meta_var::MetaVariableID, Rule>,
) -> RResult<RapidSet<String>> {
for rule in constraints.values() {
for var in rule.defined_vars() {
Expand All @@ -125,9 +125,9 @@ fn check_var_in_constraints<'r>(
Ok(vars)
}

fn check_var_in_transform<'r>(
fn check_var_in_transform(
mut vars: RapidSet<String>,
transform: &'r Option<Transform>,
transform: &Option<Transform>,
) -> RResult<RapidSet<String>> {
let Some(transform) = transform else {
return Ok(vars);
Expand Down
Loading