Skip to content

ui-react: a streaming answer re-parses its whole text on every frame #2588

Closed

Description

@schlessera

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 by memo. 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.

  • 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 (holdOpenLink and the block cuts, packages/ui-react/src/components/chat/markdown-content.tsx:31-32; entity processing before the parse in BrainMarkdownInner).
  • Tool-view markdown (tool-views.tsx:319, :428), the file viewer and share rendering, beyond inheriting the change through BrainMarkdown.

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 lint

Contract impact

  • This touches the integration contract, so it needs the contract label, a CONTRACT: 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

  1. added
    type: fix
    area: ui-react
    priority: p1
    agent-ready
    on Oct 22, 2026
  2. self-assigned this
    on Nov 17, 2026
  3. schlessera commented on Nov 17, 2026

    @schlessera
    OwnerAuthor

    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.

  4. added this to the 0.45.0 milestone on Nov 17, 2026
  5. schlessera commented on Nov 18, 2026

    @schlessera
    OwnerAuthor

    Shipped in #4240 (squash c2870f8, milestone 0.45.0).

    What changed:

    • A streaming text part is now split into top-level markdown blocks. Each block renders through a memoized parse, so only the growing last block re-parses.
    • That last block re-parses at deferred priority, behind input and scrolling, with a 200 ms lag bound.
    • A range render, and any text that cannot be split safely, still parses whole. That covers link or footnote definitions, HTML lines, nested fences and carriage returns.

    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:

    • Split and whole renders give identical HTML for a fixture corpus and for 15,000 generated documents.
    • The 24 KB append test and the Mermaid identity test both failed before the change.
    • The PR took 13 fresh-context review rounds, the last one clean.

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

agent-readyarea: ui-reactpriority: p1type: fix

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions