Skip to content

Commit 8e10335

Browse files
perf(mobile): reuse completed code lines while streaming
1 parent 6a961ad commit 8e10335

9 files changed

Lines changed: 366 additions & 86 deletions

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

Lines changed: 86 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
import { createContext, useContext, useEffect, useState } from "react";
1+
import { createContext, memo, useContext, useEffect, useMemo, useState } 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";
77
import { nativeMarkdownDocumentRuns, nativeMarkdownListItemBlocks } from "./nativeMarkdownText";
8+
import { pendingCodeHighlight } from "./pendingCodeHighlight";
89
import { NativeMarkdownSelectableText } from "./NativeMarkdownSelectableText";
910
import type {
1011
MarkdownCodeHighlighter,
@@ -29,7 +30,7 @@ const MONO_FONT_FAMILY = Platform.select({
2930
});
3031

3132
function nodeKey(node: MarkdownNode, index: number): string {
32-
return `${node.type}:${node.beg ?? index}:${node.end ?? index}`;
33+
return `${node.type}:${node.beg ?? index}`;
3334
}
3435

3536
/** Code inside markdown scales with the base text size (12pt at the default 15pt body). */
@@ -93,6 +94,7 @@ function loadHighlightedCode(
9394
language: string | undefined,
9495
theme: "light" | "dark",
9596
highlightCode: MarkdownCodeHighlighter,
97+
session: object,
9698
): Promise<HighlightedCode> {
9799
const key = codeHighlightCacheKey(code, language, theme);
98100
const cached = highlightedCodeCache.get(key);
@@ -105,7 +107,7 @@ function loadHighlightedCode(
105107
return pending;
106108
}
107109

108-
const promise = highlightCode({ code, language, theme })
110+
const promise = highlightCode({ code, language, theme, session })
109111
.then((tokens) => {
110112
cacheHighlightedCode(key, tokens);
111113
highlightedCodePromiseCache.delete(key);
@@ -125,116 +127,130 @@ function useHighlightedCode(
125127
theme: "light" | "dark",
126128
highlightCode: MarkdownCodeHighlighter,
127129
): HighlightedCode | null {
130+
const [session] = useState(() => ({}));
128131
const key = codeHighlightCacheKey(code, language, theme);
132+
const ready = useMemo(
133+
() => highlightedCodeCache.get(key) ?? highlightCode.read?.({ code, language, theme, session }),
134+
[code, language, theme, key, highlightCode, session],
135+
);
129136
const [highlighted, setHighlighted] = useState<{
130137
readonly key: string;
138+
readonly code: string;
139+
readonly language: string | undefined;
140+
readonly theme: "light" | "dark";
131141
readonly tokens: HighlightedCode | null;
132142
}>(() => ({
143+
code,
144+
language,
145+
theme,
133146
key,
134147
tokens: highlightedCodeCache.get(key) ?? null,
135148
}));
136149

137150
useEffect(() => {
151+
if (ready) return;
138152
let active = true;
139153
const cached = highlightedCodeCache.get(key);
140154
if (cached) {
141155
cacheHighlightedCode(key, cached);
142-
setHighlighted({ key, tokens: cached });
156+
setHighlighted({ code, language, theme, key, tokens: cached });
143157
return () => {
144158
active = false;
145159
};
146160
}
147161

148-
void loadHighlightedCode(code, language, theme, highlightCode)
162+
void loadHighlightedCode(code, language, theme, highlightCode, session)
149163
.then((tokens) => {
150164
if (active) {
151-
setHighlighted({ key, tokens });
165+
setHighlighted({ code, language, theme, key, tokens });
152166
}
153167
})
154168
.catch(() => {
155169
if (active) {
156-
setHighlighted({ key, tokens: null });
170+
setHighlighted({ code, language, theme, key, tokens: null });
157171
}
158172
});
159173
return () => {
160174
active = false;
161175
};
162-
}, [code, highlightCode, key, language, theme]);
176+
}, [code, highlightCode, key, language, theme, ready, session]);
163177

164-
return highlighted.key === key ? highlighted.tokens : null;
178+
if (ready) return ready;
179+
if (highlighted.key === key) return highlighted.tokens;
180+
if (highlighted.tokens && highlighted.language === language && highlighted.theme === theme) {
181+
return pendingCodeHighlight(highlighted.code, code, highlighted.tokens);
182+
}
183+
return null;
165184
}
166185

167-
function HighlightedCodeText(props: {
168-
readonly content: string;
169-
readonly highlighted: HighlightedCode | null;
170-
readonly textStyle: NativeMarkdownTextStyle;
186+
const HighlightedCodeLine = memo(function HighlightedCodeLine(props: {
187+
readonly tokens: ReadonlyArray<MarkdownHighlightedToken>;
188+
readonly color: string;
189+
readonly newline: boolean;
171190
}) {
172-
if (!props.highlighted) {
173-
return (
191+
let offset = 0;
192+
const children = [];
193+
for (const token of props.tokens) {
194+
if (!token.content) continue;
195+
children.push(
174196
<MarkdownTextPrimitive
175-
uiTextView
176-
selectable
197+
key={offset}
177198
style={{
178-
color: props.textStyle.codeColor,
199+
color: token.color ?? props.color,
179200
fontFamily: MONO_FONT_FAMILY,
180-
fontSize: codeBlockFontSize(props.textStyle),
181-
lineHeight: codeBlockLineHeight(props.textStyle),
201+
fontStyle: token.fontStyle !== null && (token.fontStyle & 1) === 1 ? "italic" : "normal",
202+
fontWeight: token.fontStyle !== null && (token.fontStyle & 2) === 2 ? "700" : "400",
182203
}}
183204
>
184-
{props.content}
185-
</MarkdownTextPrimitive>
205+
{token.content}
206+
</MarkdownTextPrimitive>,
186207
);
208+
offset += token.content.length;
187209
}
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-
});
210+
return (
211+
<MarkdownTextPrimitive>
212+
{children}
213+
{props.newline ? "\n" : ""}
214+
</MarkdownTextPrimitive>
215+
);
216+
});
207217

218+
function HighlightedCodeText(props: {
219+
readonly content: string;
220+
readonly highlighted: HighlightedCode | null;
221+
readonly textStyle: NativeMarkdownTextStyle;
222+
}) {
223+
// The text root provides inherited styles through context. A new style object
224+
// would rerender every token even when its completed line is unchanged.
225+
const fontSize = codeBlockFontSize(props.textStyle);
226+
const lineHeight = codeBlockLineHeight(props.textStyle);
227+
const style = useMemo(
228+
() => ({
229+
color: props.textStyle.codeColor,
230+
fontFamily: MONO_FONT_FAMILY,
231+
fontSize,
232+
lineHeight,
233+
}),
234+
[props.textStyle.codeColor, fontSize, lineHeight],
235+
);
236+
let offset = 0;
237+
const lines = [];
238+
if (props.highlighted) {
239+
for (const tokens of props.highlighted) {
240+
lines.push(
241+
<HighlightedCodeLine
242+
key={offset}
243+
tokens={tokens}
244+
color={props.textStyle.codeColor}
245+
newline={lines.length + 1 < props.highlighted.length}
246+
/>,
247+
);
248+
offset += tokens.reduce((length, token) => length + token.content.length, 0) + 1;
249+
}
250+
}
208251
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-
))}
252+
<MarkdownTextPrimitive uiTextView selectable style={style}>
253+
{props.highlighted ? lines : props.content}
238254
</MarkdownTextPrimitive>
239255
);
240256
}

‎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: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -705,10 +705,9 @@ export function nativeMarkdownDocumentChunks(
705705
return;
706706
}
707707
const first = selectableNodes[0];
708-
const last = selectableNodes.at(-1);
709708
chunks.push({
710709
kind: "selectable",
711-
key: `selectable:${first?.beg ?? "start"}:${last?.end ?? "end"}`,
710+
key: `selectable:${first?.beg ?? "start"}`,
712711
node: {
713712
type: "document",
714713
children: selectableNodes,
@@ -726,7 +725,7 @@ export function nativeMarkdownDocumentChunks(
726725
flushSelectable();
727726
chunks.push({
728727
kind: "rich",
729-
key: `rich:${child.type}:${child.beg ?? index}:${child.end ?? index}`,
728+
key: `rich:${child.type}:${child.beg ?? index}`,
730729
node: child,
731730
});
732731
}
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+
});
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import type { MarkdownHighlightedToken } from "./SelectableMarkdownText.types";
2+
3+
/** Keep finished lines colored while the current line awaits highlighting. */
4+
export function pendingCodeHighlight(
5+
previousCode: string,
6+
code: string,
7+
tokens: ReadonlyArray<ReadonlyArray<MarkdownHighlightedToken>>,
8+
): ReadonlyArray<ReadonlyArray<MarkdownHighlightedToken>> | null {
9+
const end = previousCode.lastIndexOf("\n") + 1;
10+
if (!end || !code.startsWith(previousCode.slice(0, end))) return null;
11+
const completedLines = previousCode.slice(0, end).split("\n").length - 1;
12+
return [
13+
...tokens.slice(0, completedLines),
14+
...code
15+
.slice(end)
16+
.split("\n")
17+
.map((content) => [{ content, color: null, fontStyle: null }]),
18+
];
19+
}

0 commit comments

Comments
 (0)