Skip to content

Commit 78fba05

Browse files
perf(web): revisiting a thread reuses its rendered Markdown instead of parsing again (pingdotgg#27480)
Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent e57ae5a commit 78fba05

5 files changed

Lines changed: 253 additions & 41 deletions

File tree

‎apps/web/package.json‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
"hast-util-to-html": "^9.0.5",
6262
"hast-util-to-jsx-runtime": "^2.3.6",
6363
"heic-to": "^1.5.2",
64+
"html-url-attributes": "^3.0.1",
6465
"jose": "catalog:",
6566
"jsonc-parser": "3.3.1",
6667
"jszip": "3.10.1",
@@ -76,13 +77,15 @@
7677
"remark-gfm": "catalog:",
7778
"remark-math": "6.0.0",
7879
"remark-parse": "catalog:",
80+
"remark-rehype": "catalog:",
7981
"saxes": "6.0.0",
8082
"sql.js": "1.15.0",
8183
"tailwind-merge": "^3.4.0",
8284
"tiff": "7.1.3",
8385
"unbash": "^5.0.0",
8486
"unified": "catalog:",
8587
"use-sync-external-store": "^1.6.0",
88+
"vfile": "^6.0.3",
8689
"zustand": "^5.0.11"
8790
},
8891
"devDependencies": {
@@ -92,6 +95,7 @@
9295
"@tanstack/router-plugin": "^1.161.0",
9396
"@types/compression": "^1.8.1",
9497
"@types/culori": "^4.0.1",
98+
"@types/hast": "^3.0.4",
9599
"@types/mdast": "^4.0.4",
96100
"@types/react": "catalog:",
97101
"@types/react-dom": "catalog:",

‎apps/web/src/components/ChatMarkdown.tsx‎

Lines changed: 83 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,8 @@ import type {
8787
ExtraProps as ReactMarkdownExtraProps,
8888
Options as ReactMarkdownOptions,
8989
} from "react-markdown";
90-
import ReactMarkdown from "react-markdown";
9190
import { createIncrementalMarkdownPlugin } from "../markdown-incremental";
91+
import type { Pluggable, PluggableList } from "unified";
9292
import { defaultUrlTransform } from "react-markdown";
9393
import { parseAssistantCitationHref } from "@t3tools/shared/assistantCitations";
9494
import { parseComposerContextHref } from "@t3tools/shared/composerContextReferences";
@@ -189,6 +189,7 @@ import {
189189
import { resolveLinkTarget } from "../browser/browserLinkTarget";
190190
import { PullRequestLinkPreview } from "./pullRequest/PullRequestLinkPreview";
191191
import { MarkdownCodeBlock, SuspenseShikiCodeBlock } from "./chat/MarkdownCodeBlock";
192+
import { createMarkdownRenderer } from "./chat/markdownRenderer";
192193
import { splitMarkdownBlocks } from "./chat/markdownStreamBlocks";
193194
import { MarkdownMermaidCodeBlock } from "./chat/MarkdownMermaidCodeBlock";
194195
import { MarkdownTable } from "./chat/MarkdownTable";
@@ -397,6 +398,14 @@ const CHAT_MARKDOWN_LITERAL_HTML_REHYPE_PLUGINS = [
397398
rehypeTextDirection,
398399
] satisfies NonNullable<ReactMarkdownOptions["rehypePlugins"]>;
399400

401+
function markdownUrlTransform(href: string) {
402+
if (parseAssistantCitationHref(href)) return href;
403+
if (parseComposerContextHref(href)) return href;
404+
if (parseThreadLinkHref(href)) return href;
405+
if (isWindowsDrivePathHref(href)) return href;
406+
return rewriteMarkdownFileUriHref(href) ?? defaultUrlTransform(href);
407+
}
408+
400409
const NO_MARKDOWN_EDITORS: ReadonlyArray<EditorId> = [];
401410

402411
type MarkdownServerConfigFields = ReturnType<typeof markdownServerConfigFields>;
@@ -644,13 +653,6 @@ function useChatMarkdownState({
644653
});
645654
};
646655
const diffThemeName = resolveDiffThemeName(resolvedTheme);
647-
const markdownUrlTransform = (href: string) => {
648-
if (parseAssistantCitationHref(href)) return href;
649-
if (parseComposerContextHref(href)) return href;
650-
if (parseThreadLinkHref(href)) return href;
651-
if (isWindowsDrivePathHref(href)) return href;
652-
return rewriteMarkdownFileUriHref(href) ?? defaultUrlTransform(href);
653-
};
654656
const handleCopy = (event: ReactClipboardEvent<HTMLElement>) => {
655657
const selection = window.getSelection();
656658
if (!selection || selection.isCollapsed || !event.clipboardData) return;
@@ -925,7 +927,6 @@ function useChatMarkdownState({
925927
componentState,
926928
handleCopy,
927929
markdownRef,
928-
markdownUrlTransform,
929930
localMediaPreview,
930931
closeLocalMediaPreview,
931932
};
@@ -1633,29 +1634,62 @@ const CHAT_MARKDOWN_COMPONENTS = {
16331634
},
16341635
} satisfies Components;
16351636

1637+
const REMARK_MATH: Pluggable = [remarkMath, { singleDollarTextMath: true }];
1638+
// Shared lists, so every message with the same options shares a processor and its cache.
1639+
const CHAT_MARKDOWN_REMARK_PLUGIN_SETS = {
1640+
plain: CHAT_MARKDOWN_REMARK_PLUGINS,
1641+
breaks: CHAT_MARKDOWN_REMARK_PLUGINS_WITH_BREAKS,
1642+
math: [REMARK_MATH, ...CHAT_MARKDOWN_REMARK_PLUGINS],
1643+
mathBreaks: [REMARK_MATH, ...CHAT_MARKDOWN_REMARK_PLUGINS_WITH_BREAKS],
1644+
} satisfies Record<string, PluggableList>;
1645+
1646+
const markdownRenderers = new WeakMap<
1647+
PluggableList,
1648+
WeakMap<PluggableList, ReturnType<typeof createMarkdownRenderer>>
1649+
>();
1650+
1651+
function markdownRenderer(remarkPlugins: PluggableList, rehypePlugins: PluggableList) {
1652+
let byRehype = markdownRenderers.get(remarkPlugins);
1653+
if (!byRehype) markdownRenderers.set(remarkPlugins, (byRehype = new WeakMap()));
1654+
let renderer = byRehype.get(rehypePlugins);
1655+
if (!renderer) {
1656+
renderer = createMarkdownRenderer({
1657+
remarkPlugins,
1658+
rehypePlugins,
1659+
components: CHAT_MARKDOWN_COMPONENTS,
1660+
urlTransform: markdownUrlTransform,
1661+
});
1662+
byRehype.set(rehypePlugins, renderer);
1663+
}
1664+
return renderer;
1665+
}
1666+
16361667
const MarkdownSource = memo(function MarkdownSource({
16371668
text,
16381669
remarkPlugins,
16391670
rehypePlugins,
1640-
urlTransform,
1671+
shared,
1672+
cache,
16411673
}: {
16421674
text: string;
1643-
remarkPlugins: NonNullable<ReactMarkdownOptions["remarkPlugins"]>;
1644-
rehypePlugins: NonNullable<ReactMarkdownOptions["rehypePlugins"]>;
1645-
urlTransform: ReactMarkdownOptions["urlTransform"];
1675+
remarkPlugins: PluggableList;
1676+
rehypePlugins: PluggableList;
1677+
/** The plugins are one of the shared sets. */
1678+
shared: boolean;
1679+
/** The text will not change again; streamed text would only fill the cache with prefixes. */
1680+
cache: boolean;
16461681
}) {
1682+
// Per-instance plugins, such as the incremental parser, set up their state on every render.
1683+
const render = shared
1684+
? markdownRenderer(remarkPlugins, rehypePlugins)
1685+
: createMarkdownRenderer({
1686+
remarkPlugins,
1687+
rehypePlugins,
1688+
components: CHAT_MARKDOWN_COMPONENTS,
1689+
urlTransform: markdownUrlTransform,
1690+
});
16471691
return (
1648-
<ChatMarkdownTextContext value={text}>
1649-
<ReactMarkdown
1650-
remarkPlugins={remarkPlugins}
1651-
rehypePlugins={rehypePlugins}
1652-
skipHtml={false}
1653-
components={CHAT_MARKDOWN_COMPONENTS}
1654-
urlTransform={urlTransform}
1655-
>
1656-
{text}
1657-
</ReactMarkdown>
1658-
</ChatMarkdownTextContext>
1692+
<ChatMarkdownTextContext value={text}>{render(text, shared && cache)}</ChatMarkdownTextContext>
16591693
);
16601694
});
16611695

@@ -1668,14 +1702,8 @@ function ChatMarkdown({
16681702
extraRemarkPlugins = EMPTY_REMARK_PLUGINS,
16691703
...props
16701704
}: ChatMarkdownProps) {
1671-
const {
1672-
componentState,
1673-
handleCopy,
1674-
markdownRef,
1675-
markdownUrlTransform,
1676-
localMediaPreview,
1677-
closeLocalMediaPreview,
1678-
} = useChatMarkdownState({ text, inline, ...props });
1705+
const { componentState, handleCopy, markdownRef, localMediaPreview, closeLocalMediaPreview } =
1706+
useChatMarkdownState({ text, inline, ...props });
16791707
// Streaming re-renders only the last block; offsets for task edits need the whole text.
16801708
const streamingBlocks =
16811709
props.isStreaming === true &&
@@ -1693,13 +1721,26 @@ function ChatMarkdown({
16931721
const bindMarkdownRef = (node: HTMLElement | null) => {
16941722
markdownRef.current = node;
16951723
};
1696-
const remarkPlugins = [
1697-
...(lineBreaks ? CHAT_MARKDOWN_REMARK_PLUGINS_WITH_BREAKS : CHAT_MARKDOWN_REMARK_PLUGINS),
1698-
...extraRemarkPlugins,
1699-
...(incrementalParsing ? [createIncrementalMarkdownPlugin()] : []),
1700-
];
1701-
if (componentState.mathEnabled)
1702-
remarkPlugins.unshift([remarkMath, { singleDollarTextMath: true }]);
1724+
const sharedRemarkPlugins =
1725+
CHAT_MARKDOWN_REMARK_PLUGIN_SETS[
1726+
componentState.mathEnabled
1727+
? lineBreaks
1728+
? "mathBreaks"
1729+
: "math"
1730+
: lineBreaks
1731+
? "breaks"
1732+
: "plain"
1733+
];
1734+
// Extra and incremental plugins are per instance, so their output is not shared.
1735+
const uncachedPlugins = extraRemarkPlugins.length > 0 || incrementalParsing;
1736+
const remarkPlugins = uncachedPlugins
1737+
? [
1738+
...sharedRemarkPlugins,
1739+
...extraRemarkPlugins,
1740+
...(incrementalParsing ? [createIncrementalMarkdownPlugin()] : []),
1741+
]
1742+
: sharedRemarkPlugins;
1743+
const blocks = streamingBlocks ?? [text];
17031744

17041745
return (
17051746
<Root
@@ -1714,7 +1755,7 @@ function ChatMarkdown({
17141755
onCopy={handleCopy}
17151756
>
17161757
<ChatMarkdownRendererContext value={componentState}>
1717-
{(streamingBlocks ?? [text]).map((source, index) => (
1758+
{blocks.map((source, index) => (
17181759
<MarkdownSource
17191760
// oxlint-disable-next-line react/no-array-index-key -- streamed blocks only change at the end
17201761
key={index}
@@ -1725,7 +1766,8 @@ function ChatMarkdown({
17251766
? CHAT_MARKDOWN_RENDER_REHYPE_PLUGINS
17261767
: CHAT_MARKDOWN_LITERAL_HTML_REHYPE_PLUGINS
17271768
}
1728-
urlTransform={markdownUrlTransform}
1769+
shared={!uncachedPlugins}
1770+
cache={!props.isStreaming}
17291771
/>
17301772
))}
17311773
</ChatMarkdownRendererContext>
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import {
2+
CHAT_MARKDOWN_REHYPE_PLUGINS,
3+
CHAT_MARKDOWN_REMARK_PLUGINS,
4+
} from "@t3tools/shared/markdownPipeline";
5+
import { renderToStaticMarkup } from "react-dom/server";
6+
import ReactMarkdown from "react-markdown";
7+
import { describe, expect, it } from "vite-plus/test";
8+
9+
import { createMarkdownRenderer } from "./markdownRenderer";
10+
11+
// Visible in the markup, so every URL attribute must pass through it.
12+
const urlTransform = (url: string) => `${url}#seen`;
13+
14+
describe("createMarkdownRenderer", () => {
15+
const render = createMarkdownRenderer({
16+
remarkPlugins: CHAT_MARKDOWN_REMARK_PLUGINS,
17+
rehypePlugins: CHAT_MARKDOWN_REHYPE_PLUGINS,
18+
components: {},
19+
urlTransform,
20+
});
21+
22+
it.each([
23+
'# Title\n\nText with `code`, **bold**, [a link](https://example.com "t") and https://bare.example.com.',
24+
"![shot](./shot.png) and [file](file:///C:/repo/a.ts) and [bad](javascript:alert(1))",
25+
"<details><summary>More</summary>\n\nHidden <b>bold</b> <script>x()</script>\n\n</details>",
26+
"| a | b |\n| - | - |\n| <br> | [x](#frag) |\n\n- [ ] task\n- [x] done",
27+
'```ts title="a.ts"\nconst a = 1;\n```\n\n> [!WARNING]\n> careful <a>bare</a>',
28+
"Footnote[^1] and ~~strike~~\n\n[^1]: the note",
29+
])("renders what react-markdown renders: %j", (source) => {
30+
const expected = renderToStaticMarkup(
31+
<ReactMarkdown
32+
remarkPlugins={CHAT_MARKDOWN_REMARK_PLUGINS}
33+
rehypePlugins={CHAT_MARKDOWN_REHYPE_PLUGINS}
34+
urlTransform={urlTransform}
35+
>
36+
{source}
37+
</ReactMarkdown>,
38+
);
39+
const element = render(source, true);
40+
expect(renderToStaticMarkup(element)).toBe(expected);
41+
expect(render(source, true)).toBe(element);
42+
});
43+
44+
it("keeps cached messages when one far larger than the cache is rendered", () => {
45+
const element = render("Kept *message*", true);
46+
render("x".repeat(600_000), true);
47+
expect(render("Kept *message*", true)).toBe(element);
48+
});
49+
});
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
import type { Element, Nodes, Root } from "hast";
2+
import { toJsxRuntime, type Components } from "hast-util-to-jsx-runtime";
3+
import { urlAttributes } from "html-url-attributes";
4+
import type { ReactElement } from "react";
5+
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
6+
import remarkParse from "remark-parse";
7+
import remarkRehype from "remark-rehype";
8+
import { type PluggableList, unified } from "unified";
9+
import { VFile } from "vfile";
10+
11+
export type MarkdownUrlTransform = (url: string, key: string, node: Element) => string;
12+
13+
interface MarkdownRendererOptions {
14+
readonly remarkPlugins: PluggableList;
15+
readonly rehypePlugins: PluggableList;
16+
readonly components: Partial<Components>;
17+
readonly urlTransform: MarkdownUrlTransform;
18+
}
19+
20+
// Elements are immutable, so a remount (a thread switch, a row scrolled back into view)
21+
// reuses one instead of parsing again. Parsing is most of the cost of rendering markdown.
22+
// An element and the hast it keeps hold about 20 bytes per source character, so ~10 MB.
23+
const MAX_CACHED_CHARS = 500_000;
24+
const rendered = new Map<string, ReactElement>();
25+
let cachedChars = 0;
26+
let nextRendererId = 0;
27+
28+
// A short block still holds a whole element tree.
29+
const ENTRY_OVERHEAD_CHARS = 200;
30+
const entrySize = (key: string) => key.length + ENTRY_OVERHEAD_CHARS;
31+
32+
function remember(key: string, element: ReactElement) {
33+
// One huge message would otherwise evict everything else.
34+
if (entrySize(key) > MAX_CACHED_CHARS / 4) return;
35+
rendered.set(key, element);
36+
cachedChars += entrySize(key);
37+
for (const oldest of rendered.keys()) {
38+
if (cachedChars <= MAX_CACHED_CHARS) break;
39+
rendered.delete(oldest);
40+
cachedChars -= entrySize(oldest);
41+
}
42+
}
43+
44+
/** Renders markdown as react-markdown does, with one processor per plugin set. */
45+
export function createMarkdownRenderer({
46+
remarkPlugins,
47+
rehypePlugins,
48+
components,
49+
urlTransform,
50+
}: MarkdownRendererOptions) {
51+
const processor = unified()
52+
.use(remarkParse)
53+
.use(remarkPlugins)
54+
.use(remarkRehype, { allowDangerousHtml: true })
55+
.use(rehypePlugins)
56+
.freeze();
57+
const keyPrefix = `${nextRendererId++}\u0000`;
58+
59+
// Literal raw HTML that survived the rehype plugins, and URL attributes, as react-markdown treats them.
60+
const prepare = (node: Nodes) => {
61+
if (!("children" in node)) return;
62+
node.children.forEach((child, index) => {
63+
if (child.type === "raw") {
64+
node.children[index] = { type: "text", value: child.value };
65+
return;
66+
}
67+
if (child.type === "element") {
68+
for (const [key, tags] of Object.entries(urlAttributes)) {
69+
if (
70+
Object.hasOwn(child.properties, key) &&
71+
(tags === null || tags.includes(child.tagName))
72+
) {
73+
child.properties[key] = urlTransform(String(child.properties[key] || ""), key, child);
74+
}
75+
}
76+
}
77+
prepare(child);
78+
});
79+
};
80+
81+
return (text: string, cache: boolean): ReactElement => {
82+
const key = keyPrefix + text;
83+
const cached = rendered.get(key);
84+
if (cached !== undefined) {
85+
// Refresh its place in the eviction order.
86+
rendered.delete(key);
87+
rendered.set(key, cached);
88+
return cached;
89+
}
90+
const file = new VFile(text);
91+
const tree = processor.runSync(processor.parse(file), file) as Root;
92+
prepare(tree);
93+
const element = toJsxRuntime(tree, {
94+
Fragment,
95+
components,
96+
ignoreInvalidStyle: true,
97+
jsx,
98+
jsxs,
99+
passKeys: true,
100+
passNode: true,
101+
});
102+
if (cache) remember(key, element);
103+
return element;
104+
};
105+
}

0 commit comments

Comments
 (0)