webui: add early exit for unchanged content in markdown processing

Skip redundant processing when coalesced chunks result in identical
content. During rapid streaming, multiple chunks may arrive and coalesce
into pendingMarkdown while processing is ongoing. When the final
coalesced content equals what was just processed, we can skip entirely.

Also clarify the RAF yield comment: the key insight is that chunks
arriving during the yield naturally coalesce, so we always render
the latest state without explicitly tracking what to skip.
This commit is contained in:
Pascal 2026-02-02 12:00:19 +01:00
parent 1d2ff059da
commit 4fca9bfe16
1 changed files with 7 additions and 2 deletions

View File

@ -316,6 +316,11 @@
* @param markdown - The raw markdown string to process
*/
async function processMarkdown(markdown: string) {
// Early exit if content unchanged (can happen with rapid coalescing)
if (markdown === previousContent) {
return;
}
if (!markdown) {
renderedBlocks = [];
unstableBlockHtml = '';
@ -513,8 +518,8 @@
await processMarkdown(nextMarkdown);
// Yield to browser for paint before processing next batch
// This prevents UI freeze at high token rates (250+ tok/s)
// Yield to browser for paint. During this, new chunks coalesce
// into pendingMarkdown, so we always render the latest state.
if (pendingMarkdown !== null) {
await new Promise((resolve) => requestAnimationFrame(resolve));
}