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
@@ -374,3 +374,41 @@ $$\n\\pi_n(\\mathbb{S}^3) = \\begin{cases}
expect(output).toBe('Regular text with $x^2$.\n\n> Quote with $y^2$.\n\nMore text with $z^2$.');
});
});
// The fast path skips the whole protect/restore pipeline when the content has
// neither `$` nor a backslash. These pin the assumption it relies on: such
// content is returned byte-for-byte unchanged, and anything carrying a trigger
// character still goes through the full pipeline.
describe('preprocessLaTeX fast path', () => {
const untouched = [
['plain prose', 'The quick brown fox jumps over the lazy dog.'],
['headings and lists', '# Title\n\n- one\n- two\n\n## Sub\n\n1. first\n2. second'],
['fenced code without escapes', '```ts\nconst x = 1;\n```'],
['inline code and emphasis', 'Use `npm run build` with **bold** and _italic_.'],
['blockquotes', '> quoted line\n> another line'],
['tables', '| a | b |\n| --- | --- |\n| 1 | 2 |'],
['brackets and parens without escapes', 'array[0] and call(arg) and [link](/url)'],
['multi-paragraph prose', 'First para.\n\nSecond para.\n\nThird para.']
];
for (const [label, input] of untouched) {
test(`returns ${label} unchanged`, () => {
expect(preprocessLaTeX(input)).toBe(input);
});
}
test('still processes content once a trigger character is present', () => {
expect(preprocessLaTeX('inline \\(x^2\\) here')).toBe('inline $x^2$ here');
expect(preprocessLaTeX('Price: $5')).toBe('Price: \\$5');
});
test('fast path result matches the full pipeline for trigger-free content', () => {
// Appending a trigger char forces the slow path; stripping it back off
// must agree with what the fast path returned for the same prefix.
const body = 'Plain paragraph with no math.\n\nAnother paragraph here.';
const viaFastPath = preprocessLaTeX(body);
const viaFullPipeline = preprocessLaTeX(`${body}\n\n\\(z\\)`);
expect(viaFullPipeline.startsWith(viaFastPath)).toBe(true);
});
});