Skip to content

perf(markdown): parse a streaming reply in settled chunks (#1475) - #1483

Merged
currentbits merged 4 commits into
mainfrom
coder/perf-f-markdown-incremental-stream-parse-4000bf
Oct 5, 2026
Merged

currentbits merged 4 commits into
mainfrom
coder/perf-f-markdown-incremental-stream-parse-4000bf

Conversation

@currentbits

Copy link
Copy Markdown
Owner

Part of #1475, renderer finding 4 (markdown was re-parsed on every streamed push).

What changed
While a reply streams, MarkdownBody splits it into chunks at settled block boundaries (markdownChunks). Each chunk is a memoized ReactMarkdown, so a push re-parses only the last, still-growing chunk. Finished messages are one parse, as before, so the final render is exactly the old one.

The splitter is conservative:

  • Never split: inside fences, between items of the same list, or right after indented code.
  • One chunk for the whole reply: if any line starts with raw HTML, or if the reply has link-reference or footnote definitions.

Measured (test/perf/markdownStream.bench.tsx, jsdom, 100 pushes of a 45 KB reply):

per push at one-shot chunked
5 KB 9.3 ms 1.9 ms
20 KB 31.3 ms 2.4 ms
45 KB 84.2 ms 3.5 ms
total 4.55 s 0.27 s

Verified independently. The check in each case: renderToStaticMarkup of the chunked render must equal the one-shot render.

  • Real data: 600 real assistant replies from a clone of the live store, cut at 9,119 points (8,037 of which split). 0 mismatches.
  • Random fuzz: random block mixes (fences, lists, quotes, tables, HTML, setext, indented and tab code), 3 seeds × 8,000. 0 mismatches. An earlier round of this fuzz found two classes, HTML blocks and indented code followed by 3., which are fixed in 5514c63. A seeded fuzz now lives in test/markdownStream.test.tsx.
  • Suites: typecheck exits 0, and the renderer suite passes 2718 of 2718.

🤖 Generated with Claude Code

Willem and others added 3 commits October 5, 2026 20:43
The streaming assistant message was re-parsed as a whole on every push,
O(n^2) over one long answer. Split it at blank lines before column-0
blocks outside fences; each settled chunk is a memoized parse, so a push
re-parses only the last chunk. Lists that continue across blank lines,
raw-HTML-only chunks, link/footnote definitions and blank-spanning HTML
blocks don't split. Non-streaming messages stay one parse.

Bench (jsdom, 45 KB at 4 pushes/s): 4.7 s -> 0.3 s total render;
per push at 45 KB 83-89 ms -> 3.6-4.5 ms.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…dented code (#1475)

A fuzz pass found two mismatches with the one-shot render:
- an HTML block starting mid-chunk swallowed a fence line, desyncing the
  fence tracker. Any line starting with raw HTML now keeps one parse
  (3 of 600 sampled real replies have one).
- a boundary right after indented code (tab or 4+ spaces) mis-parsed a
  following non-1 ordered item. No boundary after an indented line now.

Adds a seeded 10k-mix fuzz test comparing chunked vs one-shot markup.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… load

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.

…mit (#1475)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@currentbits
currentbits merged commit 2a79ccb into main Oct 5, 2026
3 of 6 checks passed
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