CSV Raw View Caching

March 29, 2026 · View on GitHub

Overview

Eliminates per-frame heap allocation in CsvViewer::show_raw_view() by caching the raw text string and rebuilding only when the content hash changes.

Problem

show_raw_view() called self.content.to_string() every frame to pass to egui::TextEdit::multiline(). For a 10 MB CSV file at 60 fps, this caused ~600 MB/s of allocation churn and visible lag.

Key Files

  • src/markdown/csv_viewer.rsCsvViewerState (cached fields), show_raw_view() (hash-guarded rebuild), blake3_content_hash() helper

Implementation

Two fields added to CsvViewerState:

FieldTypePurpose
raw_view_textStringCached copy of content for TextEdit
raw_view_hashu64Blake3 hash (truncated) of content when text was last built

show_raw_view() computes blake3_content_hash(content) and compares with raw_view_hash. The .to_string() allocation only happens when the hash differs (content changed).

Hashing

Uses blake3::hash() truncated to u64 via from_le_bytes — consistent with the Markdown AST cache pattern (Task 4). Unlike the table-view's hash_content_bytes() which samples head/tail for speed, this hashes the full content so mid-file edits are always detected.

Cache Invalidation

raw_view_text and raw_view_hash are cleared to empty/zero by:

  • invalidate_cache()
  • set_delimiter()
  • clear_delimiter_override()

Performance

MetricBeforeAfter
Per-frame allocation (10 MB CSV)~10 MB0 bytes
Per-frame workto_string() + deallocblake3 hash compare (u64 ==)
One-time cost on content changeto_string() + blake3 hash