diff --git a/.jules/bolt.md b/.jules/bolt.md new file mode 100644 index 00000000..7d3b138d --- /dev/null +++ b/.jules/bolt.md @@ -0,0 +1,3 @@ +## 2024-08-01 - Avoid Eager Allocations in Pane Tree Traversals +**Learning:** In Rust, avoid calling `.clone()` on strings inside recursive tree traversal iterators like `.any()`. This eager allocation causes `O(N)` heap allocations when pushing down the call stack, even though the insertion only requires the string to be owned exactly once at the final insertion point. +**Action:** Pass `&str` instead of `String` during recursive lookups and defer the `.to_owned()` call strictly to the final successful match condition. diff --git a/crates/forktty-core/src/model.rs b/crates/forktty-core/src/model.rs index 3ba52a03..159ccff0 100644 --- a/crates/forktty-core/src/model.rs +++ b/crates/forktty-core/src/model.rs @@ -1307,7 +1307,7 @@ impl WorkspaceModel { .workspaces .get_mut(&workspace_id) .expect("workspace verified above"); - if !push_tab_to_leaf(&mut workspace.pane_tree, near_surface_id, new_id.clone()) { + if !push_tab_to_leaf(&mut workspace.pane_tree, near_surface_id, &new_id) { return None; } workspace.focused_surface_id = new_id.clone(); diff --git a/crates/forktty-core/src/model/pane_tree.rs b/crates/forktty-core/src/model/pane_tree.rs index 2a3f7fc5..0c943e39 100644 --- a/crates/forktty-core/src/model/pane_tree.rs +++ b/crates/forktty-core/src/model/pane_tree.rs @@ -646,12 +646,12 @@ pub(super) fn set_leaf_active_for_surface(node: &mut PaneNode, surface_id: &str) pub(super) fn push_tab_to_leaf( node: &mut PaneNode, near_surface_id: &str, - new_tab_id: SurfaceId, + new_tab_id: &str, ) -> bool { match node { PaneNode::Leaf { tabs, active } => { if tabs.iter().any(|id| id == near_surface_id) { - tabs.push(new_tab_id); + tabs.push(new_tab_id.to_owned()); *active = tabs.len() - 1; true } else { @@ -660,6 +660,6 @@ pub(super) fn push_tab_to_leaf( } PaneNode::Split { children, .. } => children .iter_mut() - .any(|child| push_tab_to_leaf(child, near_surface_id, new_tab_id.clone())), + .any(|child| push_tab_to_leaf(child, near_surface_id, new_tab_id)), } }