diff --git a/.jules/bolt.md b/.jules/bolt.md index fb3e8f1..8df63ee 100644 --- a/.jules/bolt.md +++ b/.jules/bolt.md @@ -2,3 +2,6 @@ ## 2026-04-08 - [Performance: Defer Allocation during Traversal] **Learning:** During DAG traversals, creating owned variants of identifiers (like `file.to_path_buf()`) *before* checking `visited` HashSets results in heap allocations (O(E)) for every edge instead of every visited node (O(V)). By moving the `&PathBuf` allocation strictly *after* all HashSet `contains` checks using the borrowed reference (`&Path`), we drastically reduce memory churn. **Action:** Always check `HashSet::contains` with a borrowed reference *before* creating the owned version required by `HashSet::insert`, especially in performance-critical graph traversal paths. +## 2024-05-23 - [Performance: Avoid redundant `to_path_buf` calls in Tarjan's SCC] +**Learning:** In `TarjanState` during graph traversal for finding cycles, calling `.to_path_buf()` on borrowed `&Path` values repeatedly within loops to query HashSets and HashMaps (e.g., `state.indices.get(&v.to_path_buf())`) leads to unnecessary O(E) heap allocations. Since these collections accept borrowed references (e.g., `&Path`), using the borrowed reference directly (e.g., `state.indices.get(v)`) eliminates these allocations. +**Action:** In graph algorithms like Tarjan's SCC and BFS/DFS, avoid `.to_path_buf()` and `.clone()` when checking membership (`contains`, `get`, `get_mut`) in `RapidSet` or `RapidMap`. Only allocate when transferring ownership (e.g., inserting). diff --git a/crates/flow/src/incremental/invalidation.rs b/crates/flow/src/incremental/invalidation.rs index da9ac9c..eac784f 100644 --- a/crates/flow/src/incremental/invalidation.rs +++ b/crates/flow/src/incremental/invalidation.rs @@ -358,19 +358,19 @@ impl InvalidationDetector { // Update lowlink let w_lowlink = *state.lowlinks.get(dep).unwrap(); - let v_lowlink = state.lowlinks.get_mut(&v.to_path_buf()).unwrap(); + 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(); + 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(); + 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(); diff --git a/crates/rule-engine/src/check_var.rs b/crates/rule-engine/src/check_var.rs index 9e40105..17d6fa7 100644 --- a/crates/rule-engine/src/check_var.rs +++ b/crates/rule-engine/src/check_var.rs @@ -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, - constraints: &'r RapidMap, + constraints: &RapidMap, ) -> RResult> { for rule in constraints.values() { for var in rule.defined_vars() { @@ -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, - transform: &'r Option, + transform: &Option, ) -> RResult> { let Some(transform) = transform else { return Ok(vars);