Repository navigation
perf(chat): load Prism languages on demand - #1183
Merged
Merged
Conversation
The tool-result image route answered `private, no-cache` with no validator, so every view of an image read re-downloaded it (often several MB) and the server decoded its base64 again. A session's entry id and block index name the same bytes for good: pi only appends, a migration's rewrite keeps every entry, and a context_edit leaves raw history alone. The route now answers `private, max-age=31536000, immutable`; error replies are unchanged. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Each session list rebuild read every fork and subagent file's head and tail synchronously to resolve its relation, though only the session that just ran had changed: 36 ms of blocked event loop per rebuild on a folder of 355 sessions with 137 subagents. listSessionsIncremental() hands back the same row object while a file's size and mtime hold and a new one once they change, so a WeakMap keyed by the row caches the run. The same rebuild now takes 7 ms. A failed read is not cached. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Storing a session's view snapshot JSON.stringify'd it, then summed the size of every cached snapshot by serializing each again: with eight 0.8 MB snapshots a store took 7.7 ms, at every run's end, compaction and session switch. Snapshots are never mutated in place, so the size is now measured once per snapshot and the budget sums stored sizes (1.3 ms). The unmount check for a wider cached window used nested includes; it now calls snapshotCoversLoadedEntries(), which builds a Set. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Prism tokenizes a fence in the render that shows it, about 5-10 ms per 1,000 characters of TypeScript on a desktop. A reply that finished streaming, or a session opened on a long history, highlighted every fence in one task. Within a task the first 2,000 characters of fences are still highlighted at once. The others render through the same SyntaxHighlighter as `text` (same lines, line numbers and box, no tokens) and take their language one per idle slice, newest first; Safari, without requestIdleCallback, waits 50 ms per slice. Measured box sizes do not change when the colors arrive. Opening a session with 86 fences at 4x CPU throttling: the commit that shows the messages went from 1.1 s to 0.66 s and the longest task from 1.38 s to 0.85 s; all fences had their colors 1.4 s later. The end of a streamed reply: longest commit 244 ms -> 146 ms. Co-Authored-By: Claude Opus 5.5 <[email protected]>
react-syntax-highlighter's `Prism` build imports refractor/all, which registers every Prism language (about 300) when its module is evaluated. CodeBlock and FileViewer imported it from the package index, so every page load paid for it in the first AppShell render: ~650 ms at 4x CPU throttling (~200 ms on a desktop), code on screen or not. Both now render with the core-only PrismLight (deep import, so the index and refractor/all stay out of the page's chunks). The first highlighter to mount imports refractor/all as a chunk of its own; it registers into the same core instance PrismLight uses, so every language and alias works as before. Until it has loaded, fences render as `text` and files as `plaintext` with the same lines, numbers and box. Measured at 4x throttling on a production build: - a page with no code never loads the languages chunk; - a session with code: first commit 866 -> ~260 ms and first long task 987 -> ~365 ms; the registration now runs after the first paint; - 86 fences: blocks show at 1.3 s instead of 1.8 s, all colored by 3.3 s as before, box sizes unchanged. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Conflict in AGENTS.md's sessions.md topic line: main added `lib/session-links.ts` to its files; this branch added the history-cache and highlighting files and wording. Both kept. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ism-languages-async Brings in main through the base branch. Conflict in AGENTS.md's sessions.md topic line: main added `lib/session-links.ts`; this branch added the Prism language files and wording. Both kept. Co-Authored-By: Claude Opus 5.5 <[email protected]>
#1181 landed on main as one squashed commit, so its lines in AGENTS.md, docs/agents/sessions.md and components/MermaidBlock.tsx conflicted with this branch's copy of them plus the Prism changes. Main changes nothing else in those files; keep this branch's version. Co-Authored-By: Claude Opus 5.5 <[email protected]>
agegr
changed the base branch from
perf/images-relations-viewcache-highlight
to
main
October 11, 2026 11:17
#1182 added lib/session-delete.ts to the sessions.md entry in AGENTS.md, the same line this branch rewords for the Prism languages; keep both. Co-Authored-By: Claude Opus 5.5 <[email protected]>
windli2018
pushed a commit
to aiti-ai/aiti-ai
that referenced
this pull request
Nov 6, 2026
* perf(sessions): cache tool-result images for good The tool-result image route answered `private, no-cache` with no validator, so every view of an image read re-downloaded it (often several MB) and the server decoded its base64 again. A session's entry id and block index name the same bytes for good: pi only appends, a migration's rewrite keeps every entry, and a context_edit leaves raw history alone. The route now answers `private, max-age=31536000, immutable`; error replies are unchanged. Co-Authored-By: Claude Opus 5.5 <[email protected]> * perf(sessions): read a subagent's run once per file change Each session list rebuild read every fork and subagent file's head and tail synchronously to resolve its relation, though only the session that just ran had changed: 36 ms of blocked event loop per rebuild on a folder of 355 sessions with 137 subagents. listSessionsIncremental() hands back the same row object while a file's size and mtime hold and a new one once they change, so a WeakMap keyed by the row caches the run. The same rebuild now takes 7 ms. A failed read is not cached. Co-Authored-By: Claude Opus 5.5 <[email protected]> * perf(chat): measure a cached session view once Storing a session's view snapshot JSON.stringify'd it, then summed the size of every cached snapshot by serializing each again: with eight 0.8 MB snapshots a store took 7.7 ms, at every run's end, compaction and session switch. Snapshots are never mutated in place, so the size is now measured once per snapshot and the budget sums stored sizes (1.3 ms). The unmount check for a wider cached window used nested includes; it now calls snapshotCoversLoadedEntries(), which builds a Set. Co-Authored-By: Claude Opus 5.5 <[email protected]> * perf(chat): highlight code fences in idle slices past a per-task budget Prism tokenizes a fence in the render that shows it, about 5-10 ms per 1,000 characters of TypeScript on a desktop. A reply that finished streaming, or a session opened on a long history, highlighted every fence in one task. Within a task the first 2,000 characters of fences are still highlighted at once. The others render through the same SyntaxHighlighter as `text` (same lines, line numbers and box, no tokens) and take their language one per idle slice, newest first; Safari, without requestIdleCallback, waits 50 ms per slice. Measured box sizes do not change when the colors arrive. Opening a session with 86 fences at 4x CPU throttling: the commit that shows the messages went from 1.1 s to 0.66 s and the longest task from 1.38 s to 0.85 s; all fences had their colors 1.4 s later. The end of a streamed reply: longest commit 244 ms -> 146 ms. Co-Authored-By: Claude Opus 5.5 <[email protected]> * perf(chat): load Prism languages on demand react-syntax-highlighter's `Prism` build imports refractor/all, which registers every Prism language (about 300) when its module is evaluated. CodeBlock and FileViewer imported it from the package index, so every page load paid for it in the first AppShell render: ~650 ms at 4x CPU throttling (~200 ms on a desktop), code on screen or not. Both now render with the core-only PrismLight (deep import, so the index and refractor/all stay out of the page's chunks). The first highlighter to mount imports refractor/all as a chunk of its own; it registers into the same core instance PrismLight uses, so every language and alias works as before. Until it has loaded, fences render as `text` and files as `plaintext` with the same lines, numbers and box. Measured at 4x throttling on a production build: - a page with no code never loads the languages chunk; - a session with code: first commit 866 -> ~260 ms and first long task 987 -> ~365 ms; the registration now runs after the first paint; - 86 fences: blocks show at 1.3 s instead of 1.8 s, all colored by 3.3 s as before, box sizes unchanged. Co-Authored-By: Claude Opus 5.5 <[email protected]> --------- Co-authored-by: Claude Opus 5.5 <[email protected]>
windli2018
pushed a commit
to aiti-ai/aiti-ai
that referenced
this pull request
Nov 8, 2026
* perf(sessions): cache tool-result images for good The tool-result image route answered `private, no-cache` with no validator, so every view of an image read re-downloaded it (often several MB) and the server decoded its base64 again. A session's entry id and block index name the same bytes for good: pi only appends, a migration's rewrite keeps every entry, and a context_edit leaves raw history alone. The route now answers `private, max-age=31536000, immutable`; error replies are unchanged. Co-Authored-By: Claude Opus 5.5 <[email protected]> * perf(sessions): read a subagent's run once per file change Each session list rebuild read every fork and subagent file's head and tail synchronously to resolve its relation, though only the session that just ran had changed: 36 ms of blocked event loop per rebuild on a folder of 355 sessions with 137 subagents. listSessionsIncremental() hands back the same row object while a file's size and mtime hold and a new one once they change, so a WeakMap keyed by the row caches the run. The same rebuild now takes 7 ms. A failed read is not cached. Co-Authored-By: Claude Opus 5.5 <[email protected]> * perf(chat): measure a cached session view once Storing a session's view snapshot JSON.stringify'd it, then summed the size of every cached snapshot by serializing each again: with eight 0.8 MB snapshots a store took 7.7 ms, at every run's end, compaction and session switch. Snapshots are never mutated in place, so the size is now measured once per snapshot and the budget sums stored sizes (1.3 ms). The unmount check for a wider cached window used nested includes; it now calls snapshotCoversLoadedEntries(), which builds a Set. Co-Authored-By: Claude Opus 5.5 <[email protected]> * perf(chat): highlight code fences in idle slices past a per-task budget Prism tokenizes a fence in the render that shows it, about 5-10 ms per 1,000 characters of TypeScript on a desktop. A reply that finished streaming, or a session opened on a long history, highlighted every fence in one task. Within a task the first 2,000 characters of fences are still highlighted at once. The others render through the same SyntaxHighlighter as `text` (same lines, line numbers and box, no tokens) and take their language one per idle slice, newest first; Safari, without requestIdleCallback, waits 50 ms per slice. Measured box sizes do not change when the colors arrive. Opening a session with 86 fences at 4x CPU throttling: the commit that shows the messages went from 1.1 s to 0.66 s and the longest task from 1.38 s to 0.85 s; all fences had their colors 1.4 s later. The end of a streamed reply: longest commit 244 ms -> 146 ms. Co-Authored-By: Claude Opus 5.5 <[email protected]> * perf(chat): load Prism languages on demand react-syntax-highlighter's `Prism` build imports refractor/all, which registers every Prism language (about 300) when its module is evaluated. CodeBlock and FileViewer imported it from the package index, so every page load paid for it in the first AppShell render: ~650 ms at 4x CPU throttling (~200 ms on a desktop), code on screen or not. Both now render with the core-only PrismLight (deep import, so the index and refractor/all stay out of the page's chunks). The first highlighter to mount imports refractor/all as a chunk of its own; it registers into the same core instance PrismLight uses, so every language and alias works as before. Until it has loaded, fences render as `text` and files as `plaintext` with the same lines, numbers and box. Measured at 4x throttling on a production build: - a page with no code never loads the languages chunk; - a session with code: first commit 866 -> ~260 ms and first long task 987 -> ~365 ms; the registration now runs after the first paint; - 86 fences: blocks show at 1.3 s instead of 1.8 s, all colored by 3.3 s as before, box sizes unchanged. Co-Authored-By: Claude Opus 5.5 <[email protected]> --------- Co-authored-by: Claude Opus 5.5 <[email protected]>
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.
Stacked on #1181 (it builds on that PR's highlight queue in
CodeBlock). Merge #1181 first; this PR then retargets tomain.Problem
CodeBlockandFileViewerimportedPrismfrom thereact-syntax-highlighterindex. That build importsrefractor/all, which registers every Prism language (about 300) when its module is evaluated. Every page load paid for it inside the firstAppShellrender, whether or not any code was on screen.The CPU profile of a page load at 4x throttling showed:
DFS~300 ms;register/insertBefore~70 ms.That is about 650 ms of an 866 ms first commit, or about 200 ms on a desktop.
Change
CodeBlockandFileViewerrender withreact-syntax-highlighter/dist/esm/prism-light(refractor's core).createElementis a deep import too, so the index andrefractor/allstay out of the page's chunks.lib/prism-languages.tsimportsrefractor/allonce, as a separate chunk, the first time a highlighter needs it. A failed chunk load can be retried. It registers into the same core instancePrismLightuses, so every language and alias (ts,sh,yml,py, …) works as before;lib/prism-languages.test.mjschecks this.hooks/usePrismLanguages.tsstarts the load and re-renders when it is done.CodeBlockstarts it with its first block, streaming or not, so a streamed reply usually finds the languages ready.FileViewerstarts it for any file whose language is not plain text.textand files asplaintext: the same lines, line numbers and box, without tokens.PrismAsyncwas not used: until it loads, it renders line numbers as a floated column instead of inline spans, so the code would jump when the colours arrived.Measurements (production build, 4x CPU throttle, copy of a real sessions folder)
refractor/allchunk loadedWhen code is present, the registration still runs (about 500 ms at 4x), but as its own task after the first paint, not inside it. The box sizes of all 86 fences were identical before and after colouring. The file viewer highlights a
.tsfile as before.Checks
npm test: 2940 pass.tsc --noEmitandeslint .are clean.MermaidBlock.test.mjsnow checks that a block renders numbered lines without tokens before the languages load, and highlights afterloadPrismLanguages().node e2e/run.mjs: 19/19 on the second run. The first failed at the known sidebar-toggle flake (run.mjs:492), whichmainalso hits.refractorlanguage files contain no regex lookbehind outside comments, so the lazy chunk stays parseable on Safari 16.2.🤖 Generated with Claude Code