Skip to content

Cache syntax highlighting and markdown rendering per message #501

@bug-ops

Description

@bug-ops

Parent: #499

Problem

Every frame re-runs tree-sitter syntax highlighting and pulldown_cmark markdown parsing for all visible messages. For responses with multiple code blocks, this can take tens of milliseconds per frame, compounding with the event loop issue (#500) to cause UI freezes.

Locations

  • crates/zeph-tui/src/widgets/chat.rs: render_md() called per message per frame
  • crates/zeph-tui/src/widgets/chat.rs: push_code_block_text() calls SYNTAX_HIGHLIGHTER.highlight() per code block per frame
  • crates/zeph-tui/src/widgets/chat.rs: wrap_spans() iterates char-by-char per line per frame

Fix

Add a render cache to App or ChatMessage:

  • Cache key: message content hash + terminal width
  • Cache value: pre-rendered Vec<Line<'static>>
  • Invalidate when: content changes (streaming), terminal resizes, tool expanded/compact toggles
  • Streaming messages: cache only after streaming flag becomes false

This eliminates redundant markdown parsing, syntax highlighting, and word wrapping for finalized messages.

Acceptance Criteria

  • Finalized messages are rendered from cache (no re-parsing)
  • Streaming messages still update in real-time
  • Cache invalidates on terminal resize
  • Frame time measurably decreases with many code blocks visible

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions