Skip to content

⚡ Bolt: [performance improvement] - #388

Open
Lucenx9 wants to merge 1 commit into
mainfrom
bolt-pane-tree-clone-optimization-14734846095376111947
Open

⚡ Bolt: [performance improvement]#388
Lucenx9 wants to merge 1 commit into
mainfrom
bolt-pane-tree-clone-optimization-14734846095376111947

Conversation

@Lucenx9

@Lucenx9 Lucenx9 commented Aug 3, 2026

Copy link
Copy Markdown
Owner

💡 What: Changed the return type of push_tab_to_leaf from bool to Result<(), SurfaceId> and implemented a manual loop for the PaneNode::Split arm.
🎯 Why: push_tab_to_leaf was calling .clone() on the SurfaceId string inside the .any() closure for every child of a split node, causing an O(N) heap allocation overhead when scanning the tree.
📊 Impact: Eliminates unnecessary String heap allocations on cache misses during terminal splits and tree traversal by utilizing "ownership threading" - returning the rejected ID in the Err variant to be reused in the next loop iteration.
🔬 Measurement: Validated via unit tests in forktty-core ensuring that the topological insert continues to work flawlessly. Code is linted via cargo clippy.


PR created automatically by Jules for task 14734846095376111947 started by @Lucenx9

Summary

  • push_tab_to_leaf now returns Result<(), SurfaceId>.
  • The traversal reuses rejected SurfaceId values and avoids redundant cloning and allocations.
  • WorkspaceModel::add_tab handles insertion failure through the new result type.
  • User-visible tab behavior remains unchanged.
  • No GTK, VTE, socket, security, or privacy changes.
  • Unit tests validate topological insertion. cargo clippy passes.

Co-authored-by: Lucenx9 <185146821+Lucenx9@users.noreply.github.com>
@google-labs-jules

Copy link
Copy Markdown
Contributor

👋 Jules, reporting for duty! I'm here to lend a hand with this pull request.

When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down.

I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job!

For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with @jules. You can find this option in the Pull Request section of your global Jules UI settings. You can always switch back!

New to Jules? Learn more at jules.google/docs.


For security, I will only act on instructions from the user who triggered this task.

@coderabbitai

coderabbitai Bot commented Aug 3, 2026

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

push_tab_to_leaf now transfers SurfaceId ownership through Result<(), SurfaceId>. WorkspaceModel::add_tab handles insertion errors with Result::is_err(). A learning note documents the pattern.

Changes

Tab insertion ownership flow

Layer / File(s) Summary
Result-based recursive insertion
crates/forktty-core/src/model/pane_tree.rs
push_tab_to_leaf returns Ok(()) after insertion or Err(SurfaceId) when no matching leaf exists. Recursive traversal passes the owned ID between children without cloning.
Caller handling and learning note
crates/forktty-core/src/model.rs, .jules/bolt.md
WorkspaceModel::add_tab checks the Result error state. The learning note documents the ownership-transfer pattern.

Estimated code review effort: 2 (Simple) | ~10 minutes

Possibly related PRs

  • Lucenx9/forktty#383: Uses the same Result<(), SurfaceId> propagation changes in push_tab_to_leaf and WorkspaceModel::add_tab.
  • Lucenx9/forktty#375: Describes the same ownership-transfer refactor and caller updates.
  • Lucenx9/forktty#386: Changes tab ID propagation in the same pane-tree insertion path.

Suggested labels: rust

🚥 Pre-merge checks | ✅ 5 | ❌ 1

❌ Failed checks (1 inconclusive)

Check name Status Explanation Resolution
Title check ❓ Inconclusive The title indicates a performance change but does not describe the affected Rust component or the push_tab_to_leaf refactor. Use a concise title such as rust: avoid SurfaceId cloning in push_tab_to_leaf.
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Privacy Boundary ✅ Passed The diff only changes in-memory pane-tree traversal and tab insertion; changed files add no telemetry, network calls, terminal-output persistence, or external I/O.
Terminal Command Safety ✅ Passed The diff only changes pane-tree ownership and add_tab Result handling; no PTY, socket, worktree, shell, packaging, notification, or process-execution code changed.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch bolt-pane-tree-clone-optimization-14734846095376111947

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 3fc9e2dcfe

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

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 👍 / 👎.

Comment thread .jules/bolt.md
@@ -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 👍 / 👎.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🧹 Nitpick comments (2)
crates/forktty-core/src/model.rs (1)

1310-1310: 🚀 Performance & Scalability | 🔵 Trivial | ⚡ Quick win

Pass new_id by ownership into push_tab_to_leaf.

Line [1310] calls new_id.clone() even though the helper now transfers the ID through Result<(), SurfaceId>. Pass new_id directly. After success, set focus from new_surface.id.clone(). This removes one caller-side allocation and completes the ownership-preserving optimization.

Suggested change
-        if push_tab_to_leaf(&mut workspace.pane_tree, near_surface_id, new_id.clone()).is_err() {
+        if push_tab_to_leaf(&mut workspace.pane_tree, near_surface_id, new_id).is_err() {
             return None;
         }
-        workspace.focused_surface_id = new_id.clone();
+        workspace.focused_surface_id = new_surface.id.clone();
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@crates/forktty-core/src/model.rs` at line 1310, Update the call to
push_tab_to_leaf in the surrounding workspace pane-tree flow to pass new_id by
ownership instead of cloning it. After a successful insertion, derive focus from
new_surface.id.clone() so the ownership transfer remains intact and the focus
behavior is preserved.
crates/forktty-core/src/model/pane_tree.rs (1)

650-670: 🎯 Functional Correctness | 🔵 Trivial | ⚡ Quick win

Add invariant coverage for push_tab_to_leaf ownership flows.

push_tab_to_leaf is only tested through add_tab, which covers the happy first-leaf case. Add direct model-level tests for the same-leaf first child, a later split child, nested splits, and a target missing from the tree. Assert that successful insertions only update the target leaf and that the missing-target Err value is the input SurfaceId with the pane tree unchanged.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@crates/forktty-core/src/model/pane_tree.rs` around lines 650 - 670, Add
direct model-level tests for push_tab_to_leaf covering insertion into the first
leaf, a later child of a split, nested split traversal, and a missing target.
Verify each successful case appends only to the matching leaf and updates its
active index, while the missing-target case returns the original input SurfaceId
and leaves the entire pane tree unchanged.

Source: Coding guidelines

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@crates/forktty-core/src/model.rs`:
- Line 1310: Update the call to push_tab_to_leaf in the surrounding workspace
pane-tree flow to pass new_id by ownership instead of cloning it. After a
successful insertion, derive focus from new_surface.id.clone() so the ownership
transfer remains intact and the focus behavior is preserved.

In `@crates/forktty-core/src/model/pane_tree.rs`:
- Around line 650-670: Add direct model-level tests for push_tab_to_leaf
covering insertion into the first leaf, a later child of a split, nested split
traversal, and a missing target. Verify each successful case appends only to the
matching leaf and updates its active index, while the missing-target case
returns the original input SurfaceId and leaves the entire pane tree unchanged.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: 24df1c38-41fd-4980-b37e-917b80c9e166

📥 Commits

Reviewing files that changed from the base of the PR and between 5b45feb and 3fc9e2d.

📒 Files selected for processing (3)
  • .jules/bolt.md
  • crates/forktty-core/src/model.rs
  • crates/forktty-core/src/model/pane_tree.rs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant