Repository navigation
Description
Context
Deltas reach the store once per animation frame (enqueueDelta, packages/ui-react/src/connection.ts:179-198). Each flush gives the streaming text part a new string. MarkdownContent passes that string to BrainMarkdown (MarkdownContent, packages/ui-react/src/components/chat/markdown-content.tsx:22-49), which has to re-render because its content prop changed. BrainMarkdownInner then hands the whole text to react-markdown (BrainMarkdownInner, packages/ui-react/src/components/chat/brain-markdown.tsx:93-188; the call is at :179-185). The result is a full remark-gfm parse, rehype-highlight (once it has loaded, useRehypePlugins, packages/ui-react/src/components/chat/use-markdown-highlight.ts:46-65) and a React conversion of the entire part, every frame. react-markdown keeps no memo of its own (REMARK_PLUGINS comment, same file:7-15). The memo on BrainMarkdown (brain-markdown.tsx:57-62) only spares older messages.
Measured during the review on desktop with renderToStaticMarkup, per render of one text part:
| Size | Without highlight | With highlight |
|---|---|---|
| 2 KB | 5.2 ms | 8.8 ms |
| 8 KB | 10.9 ms | 16.7 ms |
| 16 KB | 21.5 ms | 29.1 ms |
| 24 KB | 29.3 ms | 40 ms |
These are desktop figures. No Android measurement and no desktop-to-phone slowdown factor has been established (see #2587); at 24 KB the desktop cost already reaches 40 ms, close to the 50 ms long-task threshold, and a phone is expected to be slower by an unmeasured amount.
Guide performance/break-up-long-tasks: "Heavy computations or long loops can block the main thread, causing the page to become unresponsive."
Bound by the D49 link rules and holdOpenLink (#551; docs/decisions/design-kit/links.md). Classified-block cuts are D42.
Scope
-
Inside
BrainMarkdownInner, split the (already entity-processed) text into top-level markdown blocks. Render each block through a memoized block component, so a finished block whose text has not changed is skipped bymemo. Use stable index keys, so earlier blocks keep their identity as the text grows. -
Choose split points conservatively. Split only at a blank line that is outside any fenced code block (backtick or tilde, any fence length), where the next line starts at column 0 and is not:
- a list-item marker (
-,*,+,1.,1)) - a blockquote
> - a table row
| - an indented continuation
When in doubt, do not split.
- a list-item marker (
-
If the text contains a link reference definition or a footnote definition (a line matching
^ {0,3}\[[^\]]+\]:), do not split it at all, because definitions resolve across the whole document. -
Wrap only the last (growing) block's text in
useDeferredValue, so input and scrolling outrank re-parsing the tail. -
holdOpenLink,splitShareBlocks(brain-markdown-share.ts:30) and the classified-block cuts (usableBlocks,markdown-content.tsx:52-69) keep running on the full text before the split, as they do now.
Out of scope
- Moving markdown parsing to a worker, or replacing react-markdown.
- Changing the per-frame delta batching, the transcript window or
content-visibility. - Incremental parsing within a block, such as a growing code fence. The last block is re-parsed whole, so a single long growing block, or a text left unsplit because it holds definitions, still costs more per frame as it grows. The PR states this limitation rather than claiming a constant per-frame cost.
- Making the whole-text preprocessing incremental (
holdOpenLinkand the block cuts,packages/ui-react/src/components/chat/markdown-content.tsx:31-32; entity processing before the parse inBrainMarkdownInner). - Tool-view markdown (
tool-views.tsx:319,:428), the file viewer and share rendering, beyond inheriting the change throughBrainMarkdown.
Acceptance criteria
- For one fixed corpus, rendering the text split and rendering it whole produce identical HTML (
renderToStaticMarkup). The corpus contains at least:- reference-style links and footnotes
- loose and tight ordered and unordered lists, including an ordered list starting at a number other than 1
- fences with blank lines inside them, in both backtick and tilde form, including an unclosed fence (mid-stream)
- blockquotes, GFM tables, setext headings, HTML blocks, entity tags and share blocks
- Appending text to the last block of a 24 KB message does not re-render any earlier block. A test counts block-component renders (or react-markdown invocations). It fails on the tree before the change and passes after.
- A finished message renders exactly as before. The existing link, scratch-link and renderer-dispatch tests pass unchanged.
- A Mermaid block in an earlier block keeps its rendered SVG state while later text streams. This is the remount hazard noted at
brain-markdown.tsx:104-107. - The PR records before/after numbers for appending to a 24 KB answer (desktop is enough), plus a Long Animation Frames capture on an Android device if one is available.
Verification
bun run test packages/ui-react/tests # includes the new equivalence and render-count tests
node scripts/visual.mjs --project=ui-react-layout --filter=renderer-dispatch
node scripts/visual.mjs --project=ui-react-layout --filter=code-fences
bun run typecheck
bun run lintContract impact
- This touches the integration contract, so it needs the
contractlabel, aCONTRACT:commit prefix, and the doc updated in the same commit. - This changes a published package's behaviour, so it needs a changeset.
Rendering output is unchanged by design. Only the render cost changes, so the changeset is a patch note for @schlessera/brain-ui-react.
2026-10-24: removed the unsupported "3 to 5 times slower" phone multiplier (#2587 records that no Android measurement or universal slowdown factor exists) and stated the remaining large-block and preprocessing limitation explicitly.
Activity
schlessera commentedon Nov 17, 2026
Claimed for implementation. Claim token:
claim-6087b6a6. Branch:perf/2588-streaming-markdown-blocks.Collisions: closure reports only; no PR edits brain-markdown.tsx or markdown-content.tsx.
fix(ui-react): never split around a nested definition; no stale tail …
fix(ui-react): defer only a growing tail; find wrapped definition lab…
fix(ui-react): split only at CommonMark blank lines (#2588)
fix(ui-react): never split around a fence or HTML block inside a cont…
fix(ui-react): never split where a fence may sit inside an HTML block (…
fix(ui-react): parse whole any text with an HTML line; keep blank lin…
fix(ui-react): never split a text holding a carriage return (#2588)
fix(ui-react): bound the deferred tail's lag; an autolink is not HTML (…
fix(ui-react): measure the tail's lag from committed renders only (#2588
fix(ui-react): time the tail's lag from when it fell behind (#2588)
fix(ui-react): the streaming tail never steps back (#2588)
perf(ui-react): parse a streaming answer block by block (#2588) (#4240)
schlessera commentedon Nov 18, 2026
Shipped in #4240 (squash c2870f8, milestone 0.45.0).
What changed:
Measured: appending to a 25 KB answer went from a median of 23.1 ms (p90 30.3) to 2.7 ms (p90 5.3) of React commit time per append.
Proof:
Main: the squash turned main red in one visual test that read the reply synchronously after a delta (#4250). #4251 fixed it, and #4253 fixed a concurrent temp-directory race the same run surfaced. Main run 44225767895 at e2c6642 is green.
Follow-ups: #4247, #4249 and #4252. These are test-harness races found while landing, with no milestone.