Skip to content

fix(staged): memoize expensive render-loop functions in SessionModal - #616

Merged
matt2e merged 1 commit into
mainfrom
mtoohey/memoize-session-render-hotpath
Apr 14, 2026
Merged

fix(staged): memoize expensive render-loop functions in SessionModal#616
matt2e merged 1 commit into
mainfrom
mtoohey/memoize-session-render-hotpath

Conversation

@matt2e

@matt2e matt2e commented Apr 14, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Memoize renderHashtagTokens(), parseContentSegments(), and hasXmlBlocks() results in SessionModal.svelte to avoid redundant regex/string work on every reactivity update
  • Activity Monitor sampling showed these functions dominating render time: RegExp.match (40 samples), String.replace (99 samples), Map.set (17 samples), string concatenation (51 samples)
  • Caches are keyed on message content strings (which are immutable once set); the hashtag token cache is additionally invalidated when hashtagItems identity changes

Test plan

  • Open a session with user messages containing #hashtag tokens (e.g., #commit:abc123, #issue:456) and verify they still render as inline badges correctly
  • Test messages with XML blocks (<action>, <branch-history>, <launch-context> tags) still show correctly as collapsible context cards
  • Open a session with many user messages and verify the modal is more responsive when scrolling through long sessions
  • Verify no visual regressions in the session modal

🤖 Generated with Claude Code

Activity Monitor sampling revealed UI freezes caused by
renderHashtagTokens, parseContentSegments, and hasXmlBlocks re-running
their regex/string work on every reactivity update for every user
message, even when the message content hadn't changed.

Add per-content memoization caches:
- renderHashtagTokens: Map<string, string> cache, cleared when
  hashtagItems identity changes
- hasXmlBlocks: Map<string, boolean> cache via cachedHasXmlBlocks wrapper
- parseContentSegments: Map<string, ContentSegment[]> cache

Since message content is immutable once set, keying on the content string
is reliable.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@matt2e
matt2e requested review from baxen and wesbillman as code owners April 14, 2026 04:09
@matt2e
matt2e merged commit bf13332 into main Apr 14, 2026
5 checks passed
@matt2e
matt2e deleted the mtoohey/memoize-session-render-hotpath branch April 14, 2026 05:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant