warp icon indicating copy to clipboard operation
warp copied to clipboard

fix: skip tree-sitter parsing for large files to prevent memory spike

Open warp-agent-staging opened this issue 6 months ago • 0 comments

Summary

Skip tree-sitter syntax parsing for files larger than 10 MB to prevent excessive memory usage.

Problem

Sentry issue 7259255054 shows "Excessive memory usage detected" alerts on macOS stable release. Heap profile analysis reveals:

  • Tree-sitter parsing: ~6.2 GB (62%) — SyntaxTreeState::parse_text → _ts_parser_parse allocates unbounded memory via _ts_subtree_new_leaf, _ts_parser__reduce, _ts_parser__recover, stack__iter
  • Buffer text loading: ~3.3 GB (33%) — GlobalBufferModel::populate_buffer_with_read_content → Buffer::replace_all → SumTree::append_str
  • Total in-use memory: ~10.1 GB

The root cause is that SyntaxTreeState::update_internal_state_with_delta unconditionally spawns tree-sitter parsing regardless of file size. Tree-sitter's internal data structures (parse stacks, subtree nodes, recovery buffers) scale super-linearly with input size.

Changes

  • crates/editor/src/content/buffer.rs: Add BufferSnapshot::byte_len() public accessor to expose the buffer's byte length
  • crates/syntax_tree/src/lib.rs: Add MAX_SYNTAX_TREE_PARSE_BYTES constant (10 MB) and early-return guard in update_internal_state_with_delta that skips parsing for buffers exceeding this threshold

Files over 10 MB will still load and display correctly in the editor — they just won't have syntax highlighting. This is a pragmatic tradeoff: syntax highlighting for a 10 MB+ file has limited value, while the memory cost is extreme.

Testing

  • cargo check -p syntax_tree -p warp_editor ✅
  • cargo clippy -p syntax_tree -p warp_editor -- -D warnings ✅
  • cargo fmt -p syntax_tree -p warp_editor -- --check ✅

Sentry Alert

  • Issue: https://sentry.io/organizations/warpdotdev/issues/7259255054/
  • Event: 08dbe0c7381444008d2a3ac3aaa2cf09
  • Environment: mac_stable_release (macOS 26.5.0)
  • Frequency: 97 events since 2026-02-11

Conversation: https://staging.warp.dev/conversation/aacc2c1b-309d-43ed-9bff-2c7dbc4c5060 Run: https://oz.staging.warp.dev/runs/019e6d8e-638f-7a9d-90ff-2354ac924e8e

This PR was generated with Oz.

warp-agent-staging avatar May 28 '26 07:05 warp-agent-staging