Skip to content

perf(chat): load Prism languages on demand - #1183

Merged
agegr merged 9 commits into
mainfrom
perf/prism-languages-async
Oct 11, 2026
Merged

agegr merged 9 commits into
mainfrom
perf/prism-languages-async

Conversation

@agegr

@agegr agegr commented Oct 11, 2026

Copy link
Copy Markdown
Owner

Stacked on #1181 (it builds on that PR's highlight queue in CodeBlock). Merge #1181 first; this PR then retargets to main.

Problem

CodeBlock and FileViewer imported Prism from the react-syntax-highlighter index. That build imports refractor/all, which registers every Prism language (about 300) when its module is evaluated. Every page load paid for it inside the first AppShell render, whether or not any code was on screen.

The CPU profile of a page load at 4x throttling showed:

  • DFS ~300 ms;
  • module instantiation ~250 ms;
  • register/insertBefore ~70 ms.

That is about 650 ms of an 866 ms first commit, or about 200 ms on a desktop.

Change

  • Core-only renderer. CodeBlock and FileViewer render with react-syntax-highlighter/dist/esm/prism-light (refractor's core). createElement is a deep import too, so the index and refractor/all stay out of the page's chunks.
  • Languages in their own chunk. lib/prism-languages.ts imports refractor/all once, as a separate chunk, the first time a highlighter needs it. A failed chunk load can be retried. It registers into the same core instance PrismLight uses, so every language and alias (ts, sh, yml, py, …) works as before; lib/prism-languages.test.mjs checks this.
  • Re-render on load. hooks/usePrismLanguages.ts starts the load and re-renders when it is done.
    • CodeBlock starts it with its first block, streaming or not, so a streamed reply usually finds the languages ready.
    • FileViewer starts it for any file whose language is not plain text.
  • Plain until loaded. Before the languages load, fences render as text and files as plaintext: the same lines, line numbers and box, without tokens. PrismAsync was 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)

before after
New-session page (no code): refractor/all chunk loaded yes, in the main bundle no
Session with code: first commit 866 ms ~260 ms
… first long task 987 ms ~365 ms
Session with 86 fences: fences first shown 1.8 s 1.3 s
… all fences coloured ~3.2 s ~3.3 s

When 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 .ts file as before.

Checks

  • npm test: 2940 pass. tsc --noEmit and eslint . are clean.
  • MermaidBlock.test.mjs now checks that a block renders numbered lines without tokens before the languages load, and highlights after loadPrismLanguages().
  • node e2e/run.mjs: 19/19 on the second run. The first failed at the known sidebar-toggle flake (run.mjs:492), which main also hits.
  • The refractor language files contain no regex lookbehind outside comments, so the lazy chunk stays parseable on Safari 16.2.

🤖 Generated with Claude Code

agegr and others added 8 commits October 11, 2026 16:53
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
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]>
@agegr
agegr merged commit 046bc67 into main Oct 11, 2026
2 checks passed
@agegr
agegr deleted the perf/prism-languages-async branch October 29, 2026 13:01
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]>
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