Skip to content

fix: lazy editor instantiation for collapsed diff files (APP-4518)#11285

Closed
warp-dev-github-integration[bot] wants to merge 1 commit into
masterfrom
fix/lazy-editor-instantiation-code-review-memory
Closed

fix: lazy editor instantiation for collapsed diff files (APP-4518)#11285
warp-dev-github-integration[bot] wants to merge 1 commit into
masterfrom
fix/lazy-editor-instantiation-code-review-memory

Conversation

@warp-dev-github-integration
Copy link
Copy Markdown

Description

Fix memory spike in code review diff rendering by deferring editor creation for collapsed files.

Root cause: CodeReviewView::build_view_state_for_file_diffs eagerly created full editor view stacks (LocalCodeEditorViewCodeEditorViewCommentEditorRichTextEditorViewEditorViewEditorModel) for every file in the diff set, regardless of whether the file starts collapsed. The heap profile shows this path allocating ~24 GB on a single diff load, with CommentEditor::new alone responsible for ~15.5 GB (53.6% of the 28.9 GB heap).

Fix: Implement lazy editor instantiation:

  1. Added content_at_head: Option<String> to FileState to retain the base content needed for deferred editor creation.
  2. In build_view_state_for_file_diffs, skip editor creation for files where is_expanded == false; store content_at_head for later.
  3. Added ensure_editor_for_file(), which lazily creates the editor when a file is first expanded.
  4. Hooked ensure_editor_for_file() into ToggleFileExpanded and FileSelected handlers.

Files that auto-collapse (binary, autogenerated, large diffs) no longer allocate editor stacks upfront. Editor creation is deferred until the user explicitly expands the file.

Linked Issue

Testing

  • cargo check --package warp --tests passes
  • cargo fmt -- --check passes
  • I have manually tested my changes locally with ./script/run

Agent Mode

  • Warp Agent Mode - This PR was created via Warp's AI Agent Mode

CHANGELOG-BUG-FIX: Fixed memory spike when opening the code review panel with large diffs by deferring editor creation for collapsed files

Conversation: https://staging.warp.dev/conversation/7db7db17-c650-40a9-bbc5-bb0664664588
Run: https://oz.staging.warp.dev/runs/019e4067-9f22-774d-a362-a3116b04a8a8

This PR was generated with Oz.

Defer creation of the expensive editor view stack (LocalCodeEditorView →
CodeEditorView → CommentEditor → RichTextEditorView → EditorView →
EditorModel) for diff files that start collapsed in the code review panel.

Previously, build_view_state_for_file_diffs eagerly created a full editor
stack for every file in the diff set — including binary files, autogenerated
files, and large diffs that auto-collapse. The heap profile from Sentry event
877fe4a5 shows this path allocating ~24 GB, with CommentEditor::new alone
responsible for ~15.5 GB (53.6% of a 28.9 GB heap).

The fix:
- Add a content_at_head field to FileState to retain the base content needed
  for deferred editor creation.
- In build_view_state_for_file_diffs, skip editor creation for files where
  is_expanded is false; store the content_at_head for later.
- Add ensure_editor_for_file(), which lazily creates the editor on first expand.
- Call ensure_editor_for_file() from ToggleFileExpanded and FileSelected
  handlers so editors are created on-demand when the user actually opens a file.

This reduces the upfront allocation proportionally to the fraction of collapsed
files in the diff. For diffs with many autogenerated or large files, this can
eliminate the majority of the 24 GB allocation spike.

Sentry: https://warpdotdev.sentry.io/issues/7259255054/
Linear: APP-4518

Co-Authored-By: Oz <oz-agent@warp.dev>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants