Skip to content

Commit d4f07c9

Browse files
committed
perf: throttle markdown lexer during streaming
Limit marked.lexer() to once per animation frame while streaming. Lex immediately when done to ensure final accuracy.
1 parent 5b24f1e commit d4f07c9

1 file changed

Lines changed: 28 additions & 6 deletions

File tree

src/lib/components/chat/Messages/Markdown.svelte

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
import { disableSingleTilde } from '$lib/utils/marked/strikethrough-extension';
99
import { mentionExtension } from '$lib/utils/marked/mention-extension';
1010
11+
import { onDestroy } from 'svelte';
12+
1113
import MarkdownTokens from './Markdown/MarkdownTokens.svelte';
1214
import footnoteExtension from '$lib/utils/marked/footnote-extension';
1315
import citationExtension from '$lib/utils/marked/citation-extension';
@@ -53,13 +55,33 @@
5355
]
5456
});
5557
56-
$: (async () => {
57-
if (content) {
58-
tokens = marked.lexer(
59-
replaceTokens(processResponseContent(content), model?.name, $user?.name)
60-
);
58+
let rafId = null;
59+
60+
function lexContent() {
61+
tokens = marked.lexer(
62+
replaceTokens(processResponseContent(content), model?.name, $user?.name)
63+
);
64+
}
65+
66+
// Throttle lexer to once per animation frame while streaming
67+
$: if (content) {
68+
if (done) {
69+
if (rafId) {
70+
cancelAnimationFrame(rafId);
71+
rafId = null;
72+
}
73+
lexContent();
74+
} else if (!rafId) {
75+
rafId = requestAnimationFrame(() => {
76+
rafId = null;
77+
lexContent();
78+
});
6179
}
62-
})();
80+
}
81+
82+
onDestroy(() => {
83+
if (rafId) cancelAnimationFrame(rafId);
84+
});
6385
</script>
6486
6587
{#key id}

0 commit comments

Comments
 (0)