⚡ Bolt: [performance improvement] Avoid eager scrollback buffer allocations on the UI thread - #369
Conversation
In `embedded_spawn.rs`, we were extracting `persisted_scrollback` (which can be a large string buffer up to 64KB) out of the model `Mutex` using `.clone()`. This was causing significant and unnecessary O(N) heap allocations directly on the UI thread during terminal initialization. This commit shifts the logic that requires the scrollback buffer into the critical section of the `Mutex` lock, using `.and_then()` and `.is_some_and()`. This allows us to safely borrow the large string via `.as_deref()` instead of cloning it, preventing the allocation entirely while maintaining safety and keeping the critical section small enough to avoid contention. Co-authored-by: Lucenx9 <185146821+Lucenx9@users.noreply.github.com>
|
👋 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 New to Jules? Learn more at jules.google/docs. For security, I will only act on instructions from the user who triggered this task. |
📝 WalkthroughWalkthroughEmbedded scrollback restoration and initial snapshot decisions now use lock-scoped borrowed persisted buffers instead of cloning strings. A dated guidance note documents avoiding eager clones of large UI data structures. ChangesEmbedded scrollback handling
Estimated code review effort: 2 (Simple) | ~10 minutes Possibly related PRs
Suggested labels: 🚥 Pre-merge checks | ✅ 6✅ Passed checks (6 passed)
✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
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. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 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.
Inline comments:
In `@crates/forktty-ui-gtk/src/gtk_app/controller/embedded_spawn.rs`:
- Around line 488-497: Update should_skip_initial_embedded_scrollback_snapshot
in ghostty_gtk_embed.rs to require supports_restore_scrollback() before
returning true. Ensure the embedded_spawn.rs caller only skips the initial
snapshot when restoration is supported and the existing persisted scrollback
conditions also allow it; otherwise preserve the first fresh snapshot.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro Plus
Run ID: 2cf5d622-c4df-4f5a-855a-50898c3ecf53
📒 Files selected for processing (2)
.jules/bolt.mdcrates/forktty-ui-gtk/src/gtk_app/controller/embedded_spawn.rs
| // ⚡ Bolt: Avoid eager cloning of the potentially 64KB scrollback buffer by reading it inside the lock. | ||
| let mut skip_initial_snapshot = model.lock().ok().is_some_and(|model| { | ||
| let persisted_scrollback = model | ||
| .surface(&surface_id) | ||
| .and_then(|surface| surface.persisted_scrollback.as_deref()); | ||
| should_skip_initial_embedded_scrollback_snapshot( | ||
| embedder.supports_restore_scrollback(), | ||
| persisted_scrollback, | ||
| ) | ||
| }); |
There was a problem hiding this comment.
🗄️ Data Integrity & Integration | 🟠 Major | ⚡ Quick win
Do not skip the first snapshot when restoration is unsupported.
The restore callback is only registered when supports_restore_scrollback() is true, but should_skip_initial_embedded_scrollback_snapshot in crates/forktty-ui-gtk/src/gtk_app/ghostty_gtk_embed.rs ignores that argument. On older libraries, this can discard the first fresh snapshot and leave stale persisted scrollback in the model.
Make the predicate require restore support:
Proposed fix
pub(super) fn should_skip_initial_embedded_scrollback_snapshot(
supports_restore: bool,
persisted_scrollback: Option<&str>,
) -> bool {
- let _ = supports_restore;
- persisted_scrollback.is_some_and(|text| !text.is_empty())
+ supports_restore && persisted_scrollback.is_some_and(|text| !text.is_empty())
}📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| // ⚡ Bolt: Avoid eager cloning of the potentially 64KB scrollback buffer by reading it inside the lock. | |
| let mut skip_initial_snapshot = model.lock().ok().is_some_and(|model| { | |
| let persisted_scrollback = model | |
| .surface(&surface_id) | |
| .and_then(|surface| surface.persisted_scrollback.as_deref()); | |
| should_skip_initial_embedded_scrollback_snapshot( | |
| embedder.supports_restore_scrollback(), | |
| persisted_scrollback, | |
| ) | |
| }); | |
| pub(super) fn should_skip_initial_embedded_scrollback_snapshot( | |
| supports_restore: bool, | |
| persisted_scrollback: Option<&str>, | |
| ) -> bool { | |
| supports_restore && persisted_scrollback.is_some_and(|text| !text.is_empty()) | |
| } |
🤖 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-ui-gtk/src/gtk_app/controller/embedded_spawn.rs` around lines
488 - 497, Update should_skip_initial_embedded_scrollback_snapshot in
ghostty_gtk_embed.rs to require supports_restore_scrollback() before returning
true. Ensure the embedded_spawn.rs caller only skips the initial snapshot when
restoration is supported and the existing persisted scrollback conditions also
allow it; otherwise preserve the first fresh snapshot.
💡 What
This PR refactors
embedded_spawn.rsto processpersisted_scrollbackwithout cloning it. By shifting the evaluation logic (embedded_scrollback_restore_bytesandshould_skip_initial_embedded_scrollback_snapshot) inside the model lock closures (.and_then()and.is_some_and()), we can borrow the string slice via.as_deref().🎯 Why
persisted_scrollbackis a potentially very large string buffer (configured up to 64KB lines). Currently, whenever a terminal tab is restored or initialized in embedded mode, we extract this buffer from the model using an eager.clone(). This causes a heavy, slow, and completely redundant O(N) heap allocation right on the UI thread, which blocks rendering and introduces latency.📊 Impact
🔬 Measurement
Run
cargo test -p forktty-ui-gtk --no-default-featuresto verify correctness. Profile the UI thread during startup with multiple persisted panes to see the eliminatedString::cloneallocations inembedded_spawn.PR created automatically by Jules for task 8310152555995107913 started by @Lucenx9
Summary
cargo test -p forktty-ui-gtk --no-default-features.