perf(toolpath-desktop): memoize markdown rendering per chat turn#46
Merged
Conversation
|
🔍 Preview deployed: https://d4fbf49c.toolpath.pages.dev |
eliothedeman
commented
Apr 22, 2026
Collaborator
Author
eliothedeman
left a comment
There was a problem hiding this comment.
Summary
Clean, targeted fix for #38. All three asks landed: textHtml/thinkingHtml cached on ChatTurn in tree.ts::flattenChatHead, toolDiff (with pre-split lines[]) cached alongside, and renderCard in viz.ts gets a Map-based memo keyed on id+text.
Notes
- Sanitization intact:
markdown.ts:19pipesmarked.parsethroughDOMPurify.sanitize, and both flatten-time caches and the viz memo callrenderMarkdown— no XSS regression. ChatView.svelte:135,163,168,190,213all read precomputed fields (t.textHtml,t.thinkingHtml,tool.toolDiff,diff.lines). The localtoolDiff()helper is deleted;isExpanded()now readst.toolDiffdirectly — consistent.tree.ts:131-152addstextHtml/thinkingHtml/toolDiffas required fields onChatTurn(not optional). Defaults are""/null, which matches existing null-handling in the view. Noanyintroduced.viz.ts:28-36memo is keyed onidwith a storedtextfor invalidation — correctly re-renders if body text changes for the same step across reloads. Unbounded, but documented and bounded in practice by step count.- Scope is tight: no benchmarks (#41), no
buildTreecaching (#39), no unrelated files. Only the three files called out in the issue. gh pr checks: deploy + test both green.
Verdict
LGTM (commenting since GitHub won't let me approve my own PR). Straightforward perf win that does exactly what the issue asked, preserves sanitization, and stays out of adjacent issues' lanes.
`ChatView.svelte` was calling `renderMarkdown(t.text)` inline inside the
`{#each turns}` block on every reactive tick. In a 1700-step Claude
session that can be 500+ assistant turns x [marked.parse + DOMPurify.
sanitize] per re-render, and Svelte 5's $derived doesn't memoize across
the loop, so typing in the tree filter or toggling view mode retriggered
the full pipeline.
Cache the rendered HTML on the `ChatTurn` itself at flatten time:
- `textHtml` / `thinkingHtml` precomputed via `renderMarkdown`.
- `toolDiff` (path + raw + pre-split `lines[]`) precomputed too, so the
tool-diff block stops re-running `raw.split("\n")` on every tick and
the expand-default lookup is a property read.
`renderCard` in `viz.ts` gets a module-level `Map<id, {text, html}>`
memo so repeat graph renders (selection / branch toggle / show-ts
changes) skip the markdown pipeline for unchanged bodies.
This couples `ChatTurn` to the markdown module, acceptable given
`ChatTurn` is a UI-model type.
Closes #38
a392174 to
da7d208
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
renderMarkdown(text)/renderMarkdown(thinking)output on eachChatTurnat flatten time, soChatView.sveltedropsmarked.parse + DOMPurify.sanitizefrom the{#each turns}hot path.change[k].raw(path + raw + pre-splitlines[]) on the turn so the tool-diff block stops re-runningraw.split(\"\n\")on every tick andisExpandedis a property read.Map<id, {text, html}>memo toviz.ts::renderCardso repeat graph renders (selection, branch toggle, show-ts/show-files) don't re-run the markdown pipeline on unchanged card bodies.In a 1700-step Claude session that's 500+ assistant turns x two expensive calls that used to re-run on every unrelated reactive tick. Svelte 5's
$deriveddoesn't memoize across the loop, so the only way to skip the work is to pre-bake it on the model.ChatTurnis a UI-model type so coupling it tomarkdown.tsis fine — the issue calls this out explicitly.Closes #38
Test plan
bun run check— 0 errors, 4 pre-existing warnings unrelated to this changebun run build— Vite build greencargo test -p toolpath-desktop— 13 tests passcargo tauri dev, confirm chat view still renders user text, assistant text, thinking blocks, and tool diffs correctly; confirm tree-query typing no longer feels laggy