Skip to content

Commit 7bd7f99

Browse files
perf(mobile): reuse completed code lines while streaming (#11211)
1 parent 2b7d3a4 commit 7bd7f99

11 files changed

Lines changed: 783 additions & 184 deletions

‎apps/mobile/modules/t3-markdown-text/src/NativeMarkdownBlock.tsx‎

Lines changed: 66 additions & 166 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
1-
import { createContext, useContext, useEffect, useState } from "react";
1+
import { createContext, memo, useContext, useMemo } from "react";
22
import { Image, Platform, ScrollView, Text, useColorScheme, View } from "react-native";
33
import type { MarkdownNode } from "react-native-nitro-markdown/headless";
44

55
import { CopyTextButton } from "./CopyTextButton";
66
import { MarkdownTextPrimitive } from "./MarkdownTextPrimitive";
7-
import { nativeMarkdownDocumentRuns, nativeMarkdownListItemBlocks } from "./nativeMarkdownText";
7+
import {
8+
nativeMarkdownDocumentRuns,
9+
nativeMarkdownListItemBlocks,
10+
nativeMarkdownNodePosition,
11+
} from "./nativeMarkdownText";
812
import { NativeMarkdownSelectableText } from "./NativeMarkdownSelectableText";
913
import type {
1014
MarkdownCodeHighlighter,
@@ -13,23 +17,19 @@ import type {
1317
NativeMarkdownTextStyle,
1418
SelectableMarkdownSkill,
1519
} from "./SelectableMarkdownText.types";
20+
import { useHighlightedCode, type HighlightedCode } from "./useHighlightedCode";
1621

1722
/** Set by SelectableMarkdownText so images anywhere in the block tree can use it. */
1823
export const MarkdownImageRendererContext = createContext<MarkdownImageRenderer | null>(null);
1924

20-
type HighlightedCode = ReadonlyArray<ReadonlyArray<MarkdownHighlightedToken>>;
21-
22-
const highlightedCodeCache = new Map<string, HighlightedCode>();
23-
const highlightedCodePromiseCache = new Map<string, Promise<HighlightedCode>>();
24-
const HIGHLIGHTED_CODE_CACHE_LIMIT = 64;
2525
const MONO_FONT_FAMILY = Platform.select({
2626
ios: "ui-monospace",
2727
android: "monospace",
2828
default: "monospace",
2929
});
3030

3131
function nodeKey(node: MarkdownNode, index: number): string {
32-
return `${node.type}:${node.beg ?? index}:${node.end ?? index}`;
32+
return `${node.type}:${nativeMarkdownNodePosition(node, index)}`;
3333
}
3434

3535
/** Code inside markdown scales with the base text size (12pt at the default 15pt body). */
@@ -67,174 +67,74 @@ function SelectableNode(props: {
6767
);
6868
}
6969

70-
function codeHighlightCacheKey(
71-
code: string,
72-
language: string | undefined,
73-
theme: "light" | "dark",
74-
): string {
75-
return `${theme}:${language ?? "text"}:${code}`;
76-
}
77-
78-
function cacheHighlightedCode(key: string, tokens: HighlightedCode): void {
79-
highlightedCodeCache.delete(key);
80-
highlightedCodeCache.set(key, tokens);
81-
82-
while (highlightedCodeCache.size > HIGHLIGHTED_CODE_CACHE_LIMIT) {
83-
const oldestKey = highlightedCodeCache.keys().next().value;
84-
if (oldestKey === undefined) {
85-
break;
86-
}
87-
highlightedCodeCache.delete(oldestKey);
88-
}
89-
}
90-
91-
function loadHighlightedCode(
92-
code: string,
93-
language: string | undefined,
94-
theme: "light" | "dark",
95-
highlightCode: MarkdownCodeHighlighter,
96-
): Promise<HighlightedCode> {
97-
const key = codeHighlightCacheKey(code, language, theme);
98-
const cached = highlightedCodeCache.get(key);
99-
if (cached) {
100-
return Promise.resolve(cached);
101-
}
102-
103-
const pending = highlightedCodePromiseCache.get(key);
104-
if (pending) {
105-
return pending;
106-
}
107-
108-
const promise = highlightCode({ code, language, theme })
109-
.then((tokens) => {
110-
cacheHighlightedCode(key, tokens);
111-
highlightedCodePromiseCache.delete(key);
112-
return tokens;
113-
})
114-
.catch((error) => {
115-
highlightedCodePromiseCache.delete(key);
116-
throw error;
117-
});
118-
highlightedCodePromiseCache.set(key, promise);
119-
return promise;
120-
}
121-
122-
function useHighlightedCode(
123-
code: string,
124-
language: string | undefined,
125-
theme: "light" | "dark",
126-
highlightCode: MarkdownCodeHighlighter,
127-
): HighlightedCode | null {
128-
const key = codeHighlightCacheKey(code, language, theme);
129-
const [highlighted, setHighlighted] = useState<{
130-
readonly key: string;
131-
readonly tokens: HighlightedCode | null;
132-
}>(() => ({
133-
key,
134-
tokens: highlightedCodeCache.get(key) ?? null,
135-
}));
136-
137-
useEffect(() => {
138-
let active = true;
139-
const cached = highlightedCodeCache.get(key);
140-
if (cached) {
141-
cacheHighlightedCode(key, cached);
142-
setHighlighted({ key, tokens: cached });
143-
return () => {
144-
active = false;
145-
};
146-
}
147-
148-
void loadHighlightedCode(code, language, theme, highlightCode)
149-
.then((tokens) => {
150-
if (active) {
151-
setHighlighted({ key, tokens });
152-
}
153-
})
154-
.catch(() => {
155-
if (active) {
156-
setHighlighted({ key, tokens: null });
157-
}
158-
});
159-
return () => {
160-
active = false;
161-
};
162-
}, [code, highlightCode, key, language, theme]);
163-
164-
return highlighted.key === key ? highlighted.tokens : null;
165-
}
166-
167-
function HighlightedCodeText(props: {
168-
readonly content: string;
169-
readonly highlighted: HighlightedCode | null;
170-
readonly textStyle: NativeMarkdownTextStyle;
70+
const HighlightedCodeLine = memo(function HighlightedCodeLine(props: {
71+
readonly tokens: ReadonlyArray<MarkdownHighlightedToken>;
72+
readonly color: string;
73+
readonly newline: boolean;
17174
}) {
172-
if (!props.highlighted) {
173-
return (
75+
let offset = 0;
76+
const children = [];
77+
for (const token of props.tokens) {
78+
if (!token.content) continue;
79+
children.push(
17480
<MarkdownTextPrimitive
175-
uiTextView
176-
selectable
81+
key={offset}
17782
style={{
178-
color: props.textStyle.codeColor,
83+
color: token.color ?? props.color,
17984
fontFamily: MONO_FONT_FAMILY,
180-
fontSize: codeBlockFontSize(props.textStyle),
181-
lineHeight: codeBlockLineHeight(props.textStyle),
85+
fontStyle: token.fontStyle !== null && (token.fontStyle & 1) === 1 ? "italic" : "normal",
86+
fontWeight: token.fontStyle !== null && (token.fontStyle & 2) === 2 ? "700" : "400",
18287
}}
18388
>
184-
{props.content}
185-
</MarkdownTextPrimitive>
89+
{token.content}
90+
</MarkdownTextPrimitive>,
18691
);
92+
offset += token.content.length;
18793
}
188-
const highlighted = props.highlighted;
189-
let sourceOffset = 0;
190-
const keyOccurrences = new Map<string, number>();
191-
const keyedLines = highlighted.map((line) => {
192-
const lineStart = sourceOffset;
193-
const tokens = line.map((token) => {
194-
const start = sourceOffset;
195-
sourceOffset += token.content.length;
196-
const signature = `${start}:${token.content}:${token.color ?? ""}:${token.fontStyle ?? ""}`;
197-
const occurrence = keyOccurrences.get(signature) ?? 0;
198-
keyOccurrences.set(signature, occurrence + 1);
199-
return { key: `${signature}:${occurrence}`, token };
200-
});
201-
sourceOffset += 1;
202-
return {
203-
key: `line:${lineStart}:${line.map((token) => token.content).join("")}`,
204-
tokens,
205-
};
206-
});
94+
return (
95+
<MarkdownTextPrimitive>
96+
{children}
97+
{props.newline ? "\n" : ""}
98+
</MarkdownTextPrimitive>
99+
);
100+
});
207101

102+
function HighlightedCodeText(props: {
103+
readonly content: string;
104+
readonly highlighted: HighlightedCode | null;
105+
readonly textStyle: NativeMarkdownTextStyle;
106+
}) {
107+
// The text root provides inherited styles through context. A new style object
108+
// would rerender every token even when its completed line is unchanged.
109+
const fontSize = codeBlockFontSize(props.textStyle);
110+
const lineHeight = codeBlockLineHeight(props.textStyle);
111+
const style = useMemo(
112+
() => ({
113+
color: props.textStyle.codeColor,
114+
fontFamily: MONO_FONT_FAMILY,
115+
fontSize,
116+
lineHeight,
117+
}),
118+
[props.textStyle.codeColor, fontSize, lineHeight],
119+
);
120+
let offset = 0;
121+
const lines = [];
122+
if (props.highlighted) {
123+
for (const tokens of props.highlighted) {
124+
lines.push(
125+
<HighlightedCodeLine
126+
key={offset}
127+
tokens={tokens}
128+
color={props.textStyle.codeColor}
129+
newline={lines.length + 1 < props.highlighted.length}
130+
/>,
131+
);
132+
offset += tokens.reduce((length, token) => length + token.content.length, 0) + 1;
133+
}
134+
}
208135
return (
209-
<MarkdownTextPrimitive
210-
uiTextView
211-
selectable
212-
style={{
213-
color: props.textStyle.codeColor,
214-
fontFamily: MONO_FONT_FAMILY,
215-
fontSize: codeBlockFontSize(props.textStyle),
216-
lineHeight: codeBlockLineHeight(props.textStyle),
217-
}}
218-
>
219-
{keyedLines.map((line, lineIndex) => (
220-
<MarkdownTextPrimitive key={line.key}>
221-
{line.tokens.map(({ key, token }) => (
222-
<MarkdownTextPrimitive
223-
key={key}
224-
style={{
225-
color: token.color ?? props.textStyle.codeColor,
226-
fontFamily: MONO_FONT_FAMILY,
227-
fontStyle:
228-
token.fontStyle !== null && (token.fontStyle & 1) === 1 ? "italic" : "normal",
229-
fontWeight: token.fontStyle !== null && (token.fontStyle & 2) === 2 ? "700" : "400",
230-
}}
231-
>
232-
{token.content}
233-
</MarkdownTextPrimitive>
234-
))}
235-
{lineIndex + 1 < keyedLines.length ? "\n" : ""}
236-
</MarkdownTextPrimitive>
237-
))}
136+
<MarkdownTextPrimitive uiTextView selectable style={style}>
137+
{props.highlighted ? lines : props.content}
238138
</MarkdownTextPrimitive>
239139
);
240140
}

‎apps/mobile/modules/t3-markdown-text/src/SelectableMarkdownText.types.ts‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,22 @@ export interface MarkdownHighlightedToken {
2525
readonly fontStyle: number | null;
2626
}
2727

28-
export type MarkdownCodeHighlighter = (input: {
28+
export interface MarkdownCodeHighlightInput {
29+
/** Identity of the mounted code block, for incremental highlighting. */
30+
readonly session?: object;
2931
readonly code: string;
3032
readonly language?: string | null;
3133
readonly theme: "light" | "dark";
32-
}) => Promise<ReadonlyArray<ReadonlyArray<MarkdownHighlightedToken>>>;
34+
}
35+
export interface MarkdownCodeHighlighter {
36+
(
37+
input: MarkdownCodeHighlightInput,
38+
): Promise<ReadonlyArray<ReadonlyArray<MarkdownHighlightedToken>>>;
39+
/** Optional synchronous result for a small append to an already warm block. */
40+
read?: (
41+
input: MarkdownCodeHighlightInput,
42+
) => ReadonlyArray<ReadonlyArray<MarkdownHighlightedToken>> | undefined;
43+
}
3344

3445
export interface SelectableMarkdownSkill {
3546
readonly name: string;

‎apps/mobile/modules/t3-markdown-text/src/nativeMarkdownText.ts‎

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -694,21 +694,31 @@ function containsRichBlock(node: MarkdownNode): boolean {
694694
return (node.children ?? []).some(containsRichBlock);
695695
}
696696

697+
/**
698+
* Sibling identity for React keys. A source offset survives appends while the
699+
* document streams; the child index is the fallback for offset-free nodes. The
700+
* two never share a namespace, so a positioned node cannot collide with an
701+
* offset-free sibling whose index happens to equal its offset.
702+
*/
703+
export function nativeMarkdownNodePosition(node: MarkdownNode, index: number): string {
704+
return node.beg === undefined ? `index:${index}` : `offset:${node.beg}`;
705+
}
706+
697707
export function nativeMarkdownDocumentChunks(
698708
document: MarkdownNode,
699709
): ReadonlyArray<NativeMarkdownDocumentChunk> {
700710
const chunks: NativeMarkdownDocumentChunk[] = [];
701711
let selectableNodes: MarkdownNode[] = [];
712+
let selectableStart = 0;
702713

703714
const flushSelectable = () => {
704-
if (selectableNodes.length === 0) {
715+
const first = selectableNodes[0];
716+
if (!first) {
705717
return;
706718
}
707-
const first = selectableNodes[0];
708-
const last = selectableNodes.at(-1);
709719
chunks.push({
710720
kind: "selectable",
711-
key: `selectable:${first?.beg ?? "start"}:${last?.end ?? "end"}`,
721+
key: `selectable:${nativeMarkdownNodePosition(first, selectableStart)}`,
712722
node: {
713723
type: "document",
714724
children: selectableNodes,
@@ -719,14 +729,17 @@ export function nativeMarkdownDocumentChunks(
719729

720730
for (const [index, child] of (document.children ?? []).entries()) {
721731
if (!containsRichBlock(child)) {
732+
if (selectableNodes.length === 0) {
733+
selectableStart = index;
734+
}
722735
selectableNodes.push(child);
723736
continue;
724737
}
725738

726739
flushSelectable();
727740
chunks.push({
728741
kind: "rich",
729-
key: `rich:${child.type}:${child.beg ?? index}:${child.end ?? index}`,
742+
key: `rich:${child.type}:${nativeMarkdownNodePosition(child, index)}`,
730743
node: child,
731744
});
732745
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { expect, it } from "vite-plus/test";
2+
import { pendingCodeHighlight } from "./pendingCodeHighlight";
3+
4+
it("keeps completed colors and exact current text without retaining an edited tail", () => {
5+
const colored = [
6+
[{ content: "const n = 1;", color: "red", fontStyle: 0 }],
7+
[{ content: "partial", color: "blue", fontStyle: 0 }],
8+
];
9+
const result = pendingCodeHighlight(
10+
"const n = 1;\npartial",
11+
"const n = 1;\nchanged\nnext",
12+
colored,
13+
)!;
14+
expect(result[0]).toBe(colored[0]);
15+
expect(result.map((line) => line.map((token) => token.content).join("")).join("\n")).toBe(
16+
"const n = 1;\nchanged\nnext",
17+
);
18+
expect(
19+
result
20+
.slice(1)
21+
.flat()
22+
.every((token) => token.color === null),
23+
).toBe(true);
24+
expect(
25+
pendingCodeHighlight("const n = 1;\npartial", "const n = 2;\npartial", colored),
26+
).toBeNull();
27+
expect(pendingCodeHighlight("partial", "other", colored)).toBeNull();
28+
});

0 commit comments

Comments
 (0)