Skip to content

Commit 500cde8

Browse files
committed
perf: throttle markdown lexer during streaming
Throttle to 50ms while streaming so the lexer isn't re-parsing on every token.
1 parent 412e391 commit 500cde8

1 file changed

Lines changed: 28 additions & 2 deletions

File tree

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

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<script>
2+
import { onDestroy } from 'svelte';
23
import { marked } from 'marked';
34
import { replaceTokens, processResponseContent } from '$lib/utils';
45
import { user } from '$lib/stores';
@@ -53,13 +54,38 @@
5354
]
5455
});
5556
56-
$: (async () => {
57+
const LEXER_THROTTLE_MS = 50;
58+
let lexerThrottleTimer;
59+
60+
function updateTokens() {
5761
if (content) {
5862
tokens = marked.lexer(
5963
replaceTokens(processResponseContent(content), model?.name, $user?.name)
6064
);
6165
}
62-
})();
66+
}
67+
68+
$: if (content) {
69+
if (done) {
70+
// Final content, render now
71+
clearTimeout(lexerThrottleTimer);
72+
updateTokens();
73+
} else {
74+
// Throttle during streaming
75+
if (!lexerThrottleTimer) {
76+
updateTokens();
77+
}
78+
clearTimeout(lexerThrottleTimer);
79+
lexerThrottleTimer = setTimeout(() => {
80+
lexerThrottleTimer = null;
81+
updateTokens();
82+
}, LEXER_THROTTLE_MS);
83+
}
84+
}
85+
86+
onDestroy(() => {
87+
clearTimeout(lexerThrottleTimer);
88+
});
6389
</script>
6490
6591
{#key id}

0 commit comments

Comments
 (0)