warp icon indicating copy to clipboard operation
warp copied to clipboard

fix: cache SyntaxSet and Theme as global singleton in NotebookCommand (APP-4552)

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

Description

Fix memory spike caused by repeated SyntaxSet::load_defaults_newlines() deserialization in NotebookCommand::new (APP-4552).

Root cause: Every time a NotebookCommand is created for a code block in a notebook, SyntaxSet::load_defaults_newlines() is called, which deserializes the entire default syntect syntax set from bincode. This is a very large allocation (~hundreds of MB per call). The Theme is also reloaded from bundled assets each time. With multiple code blocks or re-creation of models, this contributes ~1.7 GB of cumulative heap allocations visible in Sentry heap profiles.

Fix: Cache the (SyntaxSet, Theme) pair in a global OnceLock<Arc<...>> singleton so it is loaded once and shared across all NotebookCommand instances via Arc. The parse_code_into_style_ranges function now takes Arc<(SyntaxSet, Theme)> instead of owned values, avoiding deep clones.

This is complementary to PR #11348, which addresses the per-character SumTree rebuild in color_code_block_ranges_internal — a separate hot path in the same Sentry issue.

Linked Issue

  • APP-4552
  • Sentry: https://sentry.io/organizations/warpdotdev/issues/7259255054/

Testing

  • [x] cargo check -p warp --lib passes
  • [x] cargo clippy -p warp --lib -- -D warnings passes
  • [x] cargo fmt passes

Heap profile evidence

Top allocation sites from Sentry event 12b20f99:

  • Stack #1 (16.9%): SyntaxSet::load_defaults_newlines → bincode deserialization of Vec<SyntaxReference> — 1.36 GB
  • Stack #6 (1.4%): bincode string growth during SyntaxReference deserialization — 111 MB
  • Stack #8 (1.2%): HashMap capacity allocation during SyntaxSet deserialization — 98.6 MB
  • Stack #11 (1.0%): Vec<SyntaxReference> allocation — 80 MB
  • Stack #14 (0.7%): ThemeSet::load_from_reader ScopeSelectors parsing — 54.5 MB

Agent Mode

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

CHANGELOG-BUG-FIX: Fixed excessive memory usage from repeated syntax set deserialization when creating notebook code blocks.

Conversation: https://staging.warp.dev/conversation/a5dd995c-8b20-4f86-a8ed-9e85e6f2da31 Run: https://oz.staging.warp.dev/runs/019e6cb2-4dc6-7aa7-aaae-f70c00fd8c92

This PR was generated with Oz.

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