From bbe60253b8e7aad9dce7129e9e87e64c9aaf48cd Mon Sep 17 00:00:00 2001 From: Alain Schlesser Date: Tue, 17 Nov 2026 21:09:06 +0000 Subject: [PATCH 01/14] perf(ui-react): parse a streaming answer block by block (#2588) Each animation frame gave the streaming text part a new string, and BrainMarkdownInner handed the whole of it to react-markdown: a full remark-gfm parse, highlight and React conversion of the part, every frame. The processed text is now split into top-level blocks, each parsed by a memoized block component with a stable index key, so a finished block is skipped while the answer grows; only the growing last block re-parses, and its re-parse is deferred behind input. Rendered HTML is unchanged: blocks rejoin with react-markdown's own newline, and the split is conservative (never inside a fence, before a list, blockquote, table or indented continuation, nor in a text holding a definition or a long HTML block). A projected range still parses whole. Two browser tests read a streamed fence right after a synchronous render; they now wait for the deferred render of the growing block. Co-Authored-By: Claude Opus 5.5 --- .changeset/2588-streaming-markdown-blocks.md | 5 ++ .../src/components/chat/brain-markdown.tsx | 47 +++++++++-- .../src/components/chat/markdown-blocks.ts | 68 +++++++++++++++ .../browser/code-fences-plain.layout.tsx | 3 +- .../browser/renderer-dispatch.layout.tsx | 3 +- .../ui-react/tests/markdown-blocks.test.tsx | 71 ++++++++++++++++ .../tests/render/streaming-markdown-dom.ts | 16 ++++ .../tests/render/streaming-markdown.test.tsx | 83 +++++++++++++++++++ 8 files changed, 286 insertions(+), 10 deletions(-) create mode 100644 .changeset/2588-streaming-markdown-blocks.md create mode 100644 packages/ui-react/src/components/chat/markdown-blocks.ts create mode 100644 packages/ui-react/tests/markdown-blocks.test.tsx create mode 100644 packages/ui-react/tests/render/streaming-markdown-dom.ts create mode 100644 packages/ui-react/tests/render/streaming-markdown.test.tsx diff --git a/.changeset/2588-streaming-markdown-blocks.md b/.changeset/2588-streaming-markdown-blocks.md new file mode 100644 index 0000000000..ca8b55854f --- /dev/null +++ b/.changeset/2588-streaming-markdown-blocks.md @@ -0,0 +1,5 @@ +--- +"@schlessera/brain-ui-react": patch +--- + +A streaming answer no longer re-parses its whole text on every frame. Each top-level markdown block now parses on its own, and a finished block is skipped while the answer grows, so only the growing last block re-parses, and its re-parse is deferred behind input and scrolling. Appending to a 24 KB answer went from a 23 ms median render to under 3 ms (desktop). What renders is unchanged. A text holding a link reference or footnote definition, or an HTML block that may contain blank lines, still parses whole, as does a single long growing block. diff --git a/packages/ui-react/src/components/chat/brain-markdown.tsx b/packages/ui-react/src/components/chat/brain-markdown.tsx index c05c55efe2..bcaabf70d5 100644 --- a/packages/ui-react/src/components/chat/brain-markdown.tsx +++ b/packages/ui-react/src/components/chat/brain-markdown.tsx @@ -1,7 +1,8 @@ import { MarkdownRangeContext, rehypeMarkdownRange } from "./markdown-range.js"; import { useBrainUiRoot } from "../../root-context.js"; -import React, { memo, useContext, useEffect, useMemo } from "react"; +import React, { memo, useContext, useDeferredValue, useEffect, useMemo } from "react"; import Markdown from "react-markdown"; +import { splitMarkdownBlocks } from "./markdown-blocks.js"; import { classifyRepoPath, @@ -175,15 +176,45 @@ const BrainMarkdownInner = memo(function BrainMarkdownInner({ content, className }; }, [entityTags, fileLinks, root]); + // A projected range maps offsets into the whole text, so it parses whole. + // Otherwise each top-level block parses on its own (#2588): a streaming + // answer grows only at its end, so its finished blocks keep their text and + // `MarkdownBlock`'s memo skips them, and only the last one re-parses. + const blocks = useMemo(() => range ? [processed] : splitMarkdownBlocks(processed), [processed, range]); return (
- - {processed} - + + {blocks.map((text, i) => ( + + ))} +
); }); + +type BlockProps = { text: string; rehypePlugins: React.ComponentProps["rehypePlugins"]; components: React.ComponentProps["components"] }; + +/** + * One top-level block's parse. Memoized on its text and the (memoized) + * plugins and components, so a finished block is not re-parsed while the + * answer streams on, and its elements (a rendered Mermaid diagram) keep + * their state: its key is its index, which a growing text never changes. + */ +const MarkdownBlock = memo(function MarkdownBlock({ text, rehypePlugins, components }: BlockProps) { + return {text}; +}); + +/** + * A block in its place. Every block is this one component type, so the last + * block becoming an earlier one (a new block starts below it) changes no + * element type and remounts nothing. Only the last block's text is deferred, + * so input and scrolling outrank re-parsing the growing tail. + */ +function StreamedBlock({ text, last, ...rest }: BlockProps & { last: boolean }) { + const deferred = useDeferredValue(text); + return <> + + {/* react-markdown separates top-level blocks with a newline; keep it between ours. */} + {last ? null : "\n"} + ; +} diff --git a/packages/ui-react/src/components/chat/markdown-blocks.ts b/packages/ui-react/src/components/chat/markdown-blocks.ts new file mode 100644 index 0000000000..640286db61 --- /dev/null +++ b/packages/ui-react/src/components/chat/markdown-blocks.ts @@ -0,0 +1,68 @@ +/** + * Top-level markdown blocks of one text part, for rendering each through its + * own memoized parse (#2588). A streaming answer grows only at its end, so + * a finished block whose text has not changed is skipped by `memo` instead + * of re-parsed on every frame. + * + * Splitting must never change what renders: each block parsed alone, joined + * in order, must give the same HTML as the whole text parsed once. So the + * split points are conservative, and when in doubt the text is not split: + * + * - only at a blank line outside every fenced code block (backtick or tilde, + * any fence length, an unclosed fence running to the end); + * - only where the next line starts at column 0 and does not continue a + * list (`-`, `*`, `+`, `1.`, `1)`), a blockquote (`>`) or a table (`|`); + * - never in a text holding a link reference or footnote definition, which + * resolves across the whole document; + * - never in a text holding an HTML block that may contain blank lines + * (`
`, `