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 @@
## 2024-08-03 - [Avoid clone in pane_tree's push_tab_to_leaf]

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Remove the one-off .jules root bucket

This introduces a new top-level directory solely for a note about one private pane-tree helper; a repo-wide search finds no consumer or generator for it, and the repository previously removed a stale file at this same path. Keep implementation rationale beside the owning helper or omit this automation diary rather than creating a non-durable root content category.

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

Useful? React with 👍 / 👎.

**Learning:** In Rust performance optimization, avoid eagerly allocating cloned data (like `.clone()` on strings or identifiers) when passed into recursive search functions (e.g., `push_tab_to_leaf`). We can pass the value by ownership and return it back in the `Err` variant of a `Result` on a cache miss (e.g., `Result<(), SurfaceId>`).
**Action:** Use this pattern `Result<(), T>` instead of `bool` when transferring ownership into nested tree traversals. This allows the caller to reuse the same allocation for subsequent loop iterations without calling `.clone()`.
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
19 changes: 13 additions & 6 deletions crates/forktty-core/src/model/pane_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -647,19 +647,26 @@ pub(super) fn push_tab_to_leaf(
node: &mut PaneNode,
near_surface_id: &str,
new_tab_id: SurfaceId,
) -> bool {
) -> Result<(), SurfaceId> {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Update the helper's return-contract comment

The signature now returns Result<(), SurfaceId>, but the adjacent rustdoc still says it “Returns true if found” and omits the ownership-threading contract that Err returns the unused ID. This makes the helper's documented contract contradict its API and hides the invariant the optimization depends on; document Ok(()) on insertion and Err(new_tab_id) on a miss.

AGENTS.md reference: AGENTS.md:L171-L171

Useful? React with 👍 / 👎.

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 = new_tab_id;
for child in children.iter_mut() {
match push_tab_to_leaf(child, near_surface_id, current) {
Ok(()) => return Ok(()),
Err(returned) => current = returned,
}
}
Err(current)
}
}
}
Loading