Skip to content

fix: prevent multi-GiB memory spikes in editor for large files (APP-4519)#11397

Draft
warp-dev-github-integration[bot] wants to merge 1 commit into
masterfrom
fix/memory-spike-large-file-editor-APP-4519
Draft

fix: prevent multi-GiB memory spikes in editor for large files (APP-4519)#11397
warp-dev-github-integration[bot] wants to merge 1 commit into
masterfrom
fix/memory-spike-large-file-editor-APP-4519

Conversation

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

Description

Prevent multi-GiB memory spikes when loading very large files into the code editor. The Sentry memory alert (APP-4519) shows 24+ GiB heap usage dominated by SumTree<BlockItem>::push (82.5%) and SumTree<BufferText>::append_str (13.7%) after a GlobalBufferModel file load.

This PR implements three defensive layers:

  1. File size guard (20 MB)FileModel and GlobalBufferModel reject files exceeding MAX_EDITOR_FILE_SIZE before reading content into the buffer. Applies to local files, remote buffers, and auto-reload paths. A user-friendly toast shows the file size and the limit.

  2. Lightweight layout (5 MiB threshold) — When an EditDelta's total content exceeds MAX_LAYOUT_BYTES, layout_text_block_lightweight creates empty TextFrames with correct font metrics instead of running expensive platform text layout (CoreText on macOS). This prevents the 19+ GiB SumTree<BlockItem> retention seen in the Sentry heap profile.

  3. Tree-sitter guard (1 MiB) — Buffers exceeding MAX_SYNTAX_TREE_BUFFER_BYTES skip tree-sitter parsing entirely, avoiding multi-GiB heap spikes from grammars with expensive external scanners.

Also removes an unnecessary .clone() on EditDelta in DelayRendering::flush_render.

Files changed

  • crates/warp_util/src/file.rs — Add MAX_EDITOR_FILE_SIZE constant and FileTooLarge error variant
  • crates/warp_files/src/lib.rs — File size check before reading in open(), read_content_for_file(), and auto-reload
  • app/src/code/global_buffer_model.rs — Remote buffer size guard in apply_open_buffer_response
  • app/src/code/view.rs — User-friendly toast for file-too-large errors
  • app/src/ai/blocklist/action_model/execute.rs — Handle FileTooLarge error variant
  • crates/editor/src/content/buffer.rs — Add byte_len() and content_byte_len() helpers
  • crates/editor/src/content/edit.rs — Add MAX_LAYOUT_BYTES, layout_text_block_lightweight, and lightweight layout dispatch
  • crates/syntax_tree/src/lib.rs — Add MAX_SYNTAX_TREE_BUFFER_BYTES guard
  • app/src/code/editor/model.rs — Remove unnecessary .clone() on EditDelta

Linked Issue

Testing

  • cargo fmt — passes

  • cargo clippy --workspace --all-targets --all-features --tests -- -D warnings — passes

  • This change addresses the root cause of the Sentry memory alert by preventing large file content from entering the editor pipeline

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

Conversation: https://staging.warp.dev/conversation/84b4c4e8-93a6-4ca5-8773-4a7454f2224d
Run: https://oz.staging.warp.dev/runs/019e4450-ddfc-7a01-8ca0-2d3f07b81511
This PR was generated with Oz.

)

Three defensive layers prevent the editor from consuming unbounded memory
when loading very large files:

1. **File size guard (20 MB)**: FileModel and GlobalBufferModel reject files
   exceeding MAX_EDITOR_FILE_SIZE (20 MB) before reading content into the
   buffer. Applies to local files, remote buffers, and auto-reload paths.
   A user-friendly toast shows the file size and the limit.

2. **Lightweight layout (5 MiB)**: When an EditDelta's total content exceeds
   MAX_LAYOUT_BYTES, layout_text_block_lightweight creates empty TextFrames
   with correct font metrics instead of running expensive platform text
   layout (CoreText on macOS). This prevents the 19+ GiB SumTree<BlockItem>
   retention seen in the Sentry heap profile.

3. **Tree-sitter guard (1 MiB)**: Buffers exceeding MAX_SYNTAX_TREE_BUFFER_BYTES
   skip tree-sitter parsing entirely, avoiding multi-GiB heap spikes from
   grammars with expensive external scanners.

Also removes an unnecessary .clone() on EditDelta in DelayRendering::flush_render.

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

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.

1 participant