Repository navigation
Description
#9228 bounds the Shiki token cache for the chat markdown path. Three other call sites still
import the default plugin from @streamdown/code and so still go through the unbounded
module-level Map that PR describes. One of them streams, and therefore leaks in exactly the same
way.
The mechanism, in short
@streamdown/code 1.1.1 memoises every tokenisation in a module-level Map with no size cap, no
clear and no unmount hook. Its key is built from the source's length plus its first and last 100
characters:
x = (e, n, t) => `${n}:${t[0]}:${t[1]}:${e.length}:${e.slice(0, 100)}:${e.slice(-100)}`
so a fence that STREAMS reaches the highlighter once per refresh window with a longer prefix each
time, and every window mints a fresh key and a fresh full dual-theme tokenisation of that prefix
that is then kept for the life of the tab. Measured on the chat path before #9228: about
0.28 MB retained per 250 ms refresh window for a 32 KB fence, so +9.21 MB for a fence that
arrived over five seconds and +82.71 MB for the same fence over eighty-three.
Note for anyone tracing this: streamdown itself is fine. Its caches are all bounded. The
unbounded one is in @streamdown/code.
The call sites
studio/frontend/src/components/assistant-ui/tool-code-cell.tsx:10 - this is the one that
matters.
import { code as codePlugin } from "@streamdown/code";
That surface streams (the file has its own follow-the-stream pinning logic), so it leaks
identically to the chat path that #9228 just fixed. Any long tool output rendered as a code cell
pays the same per-refresh-window cost.
Low priority, static content, one entry each:
studio/frontend/src/components/markdown/markdown-preview.tsx:9studio/frontend/src/features/hub/catalog/model-readme.tsx:17
These render content that arrives whole rather than a token at a time, so they mint one cache entry
per document rather than one per refresh window. They are worth tidying for consistency, not for
memory.
Suggested shape of the fix
#9228 already contains the machinery. code-plugin.ts has a tokenize() with the same engine,
the same language resolution and the same "cached result synchronously, else null plus a
callback" contract as the package, backed by code-token-cache.ts, which is bounded and evicts the
prefixes of a fence as the fence grows.
What the chat path additionally has, and what these surfaces should NOT inherit, is the per-fence
slot throttle in createCodePlugin. That throttle reuses cached tokens with an unstyled tail
between refreshes, which is a deliberate chat behaviour and would change what a tool cell shows
while it streams. So the extension is to export the unthrottled shape, roughly
{ ...inner, highlight: tokenize }, and import that here instead of the package default. Behaviour
identical to today, memory bounded.
Why it was left out of #9228
Deliberately, not by oversight. Every claim in that PR is backed by a measurement on the chat path,
including byte-for-byte rendered-output parity across two checkouts. Extending it to a surface that
had neither measured nor parity-checked would have put an unverified change inside an otherwise
fully verified one. The right order is to measure these surfaces on their own and then extend.
What a fix here needs to show
The harnesses from #9228 are reusable, but they drive the chat fixture page, so this needs its own
fixture that renders a tool code cell:
- Retained-heap slope across N streamed tool outputs, sampled after unmount and after a forced
GC, base against head, arms interleaved, medians of paired ratios. - Byte-for-byte rendered-output parity between the two checkouts, with a negative control
proving the comparator can see a difference when there is one. - A control arm that does not touch the highlighter and must stay flat.
Do not expect a frame-rate improvement from it. The equivalent measurement on the chat path came
back null: with 223 MB retained against 9.6 MB, fps, p95 frame time, worst frame and longest stall
were all unchanged. This is a memory result, and it should be claimed as one.
Chromium only, as with #9228. Nothing measured here speaks for WKWebView or WebKitGTK, which is
what Desktop embeds on macOS and Linux and where memory pressure actually bites.
Activity
claxman commentedon Sep 9, 2026
Taking the tool-code-cell import. On main dd2b0f6
tool-code-cell.tsxline 12 still imports{ code as codePlugin } from "@streamdown/code". I will open a PR moving that cell off the direct@streamdown/codeimport.