ui: reduce per-token render cost when streaming (#26053)

* performance harness - the empirical root

Assisted-by: Claude Opus 4.8

* 210.36ms -> 2.67ms per streamed token

Assisted-by: Claude Opus 4.8

* 11.58ms -> 0.62ms per streamed token

Assisted-by: Claude Opus 4.8

* 22.02ms -> 3.33ms per streamed token

Assisted-by: Claude Opus 4.8

* 3.07ms -> 1.36ms per streamed token at 40 messages

Assisted-by: Claude Opus 4.8

---------

Co-authored-by: Zach Winter <dmtommy@icloud.com>
This commit is contained in:
Zach Winter
2026-07-24 22:09:46 +02:00
committed by GitHub
co-authored by Zach Winter
parent 96013c5112
commit 555881ebc8
18 changed files with 1037 additions and 35 deletions
@@ -107,6 +107,15 @@
return null;
});
const liveSvgHtml = $derived(streamingSvgCode !== null ? sanitizeSvg(streamingSvgCode) : '');
// Derived rather than called inline in the template so it only recomputes when
// the block actually changes. Auto-detection is disabled while streaming: it
// costs ~38ms a call and re-guesses the language on every chunk.
const streamingCodeHtml = $derived(
incompleteCodeBlock
? highlightCode(incompleteCodeBlock.code, incompleteCodeBlock.language || 'text', false)
: ''
);
let previewDialogOpen = $state(false);
let previewCode = $state('');
let previewLanguage = $state('text');
@@ -903,10 +912,7 @@
>
<pre class="streaming-code-pre"><code
class="hljs language-{incompleteCodeBlock.language || 'text'}"
>{@html highlightCode(
incompleteCodeBlock.code,
incompleteCodeBlock.language || 'text'
)}</code
>{@html streamingCodeHtml}</code
></pre>
</div>
</div>