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
3 changes: 3 additions & 0 deletions .jules/bolt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## 2026-07-28 - Avoid eager cloning in recursive search

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Remove the bot-only root metadata directory

A repo-wide search finds no consumer of .jules/bolt.md, and this commit creates .jules/ solely for a generic bot learning note. This introduces an undocumented top-level content bucket despite the repository contract reserving new root directories for durable project-content categories; omit the note or place genuinely durable documentation under an existing owner.

AGENTS.md reference: AGENTS.md:L131-L131

Useful? React with πŸ‘Β / πŸ‘Ž.

**Learning:** In Rust, to avoid redundant allocations when passing owned values (like `String` or large aliases like `SurfaceId`) into recursive search functions (e.g., tree traversals) that might fail to consume them, pass the value by ownership and return it back in the `Err` variant of a `Result` on a cache miss (e.g., `Result<(), T>`). This allows the caller to reuse the same allocation for subsequent loop iterations without calling `.clone()`.
**Action:** Use this pattern to eliminate `.clone()` calls inside `.any()` closures during tree traversals.
2 changes: 1 addition & 1 deletion crates/forktty-core/src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.clone()).is_err() {
return None;
}
workspace.focused_surface_id = new_id.clone();
Expand Down
24 changes: 18 additions & 6 deletions crates/forktty-core/src/model/pane_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -643,23 +643,35 @@ pub(super) fn set_leaf_active_for_surface(node: &mut PaneNode, surface_id: &str)

/// Push `new_tab_id` to the tabs of the leaf containing `near_surface_id`.
/// Sets `active` to the new tab's index. Returns `true` if found.
/// Push `new_tab_id` to the tabs of the leaf containing `near_surface_id`.
/// Sets `active` to the new tab's index.
///
/// Returns `Ok(())` if the insertion succeeded, or `Err(new_tab_id)` if the
/// leaf wasn't found in this branch, threading ownership back to avoid `clone()`.
pub(super) fn push_tab_to_leaf(
node: &mut PaneNode,
near_surface_id: &str,
new_tab_id: SurfaceId,
) -> bool {
) -> Result<(), SurfaceId> {
match node {
PaneNode::Leaf { tabs, active } => {
if tabs.iter().any(|id| id == near_surface_id) {
tabs.push(new_tab_id);
*active = tabs.len() - 1;
true
Ok(())
} else {
false
Err(new_tab_id)
}
}
PaneNode::Split { children, .. } => children
.iter_mut()
.any(|child| push_tab_to_leaf(child, near_surface_id, new_tab_id.clone())),
PaneNode::Split { children, .. } => {
let mut current_id = new_tab_id;
for child in children {
match push_tab_to_leaf(child, near_surface_id, current_id) {
Ok(()) => return Ok(()),
Err(id) => current_id = id,
}
}
Err(current_id)
}
}
}
Loading