Files
llama.cpp/ui/apps/chat/tests/unit/agentic-hotpath.bench.ts
T
Aleksander Grygier c6a25fc622 ui: load highlight.js languages on demand (#32661)
* ui: load highlight.js on first use

highlight.js and every lowlight language (~900 KB raw) moved out of
Llama Chat's first load into a chunk that loads with the first code block.

- @ggml-org/llama-ui/code: highlightCode stays synchronous. Until
  highlight.js has loaded it returns the code escaped as plain text and
  starts the load; a $derived or $effect that called it runs again once
  it has loaded. New loadHighlighter() and isHighlighterLoaded().
- Markdown: rehype-highlight runs through a lazy rehype step, like
  KaTeX's: the first message with a code block waits for it, every
  other one renders in the same pass.

First-load JS of the chat page: 3.47 -> 2.56 MB raw, 1.05 -> 0.75 MB
gzip, 0.84 -> 0.60 MB brotli.

Assisted-by: Claude (Opus 5.5 Medium)

* ui: load each highlight.js language on first use

highlight.js now loads one language at a time instead of all ~190 at
once with the first code block: every language is its own chunk, and
the core loads with the first one.

- @ggml-org/llama-ui/code: highlightCode looks a language or alias up
  in a generated table (scripts/generate-highlight-languages.mjs), loads
  it with the languages it embeds (CSS and JavaScript inside HTML, say)
  and returns plain text until then; a $derived or $effect that called
  it runs again once it has loaded. Auto-detection picks among lowlight's
  37 common languages, loaded the first time something auto-detects.
  New loadHighlightLanguages(), isHighlightLanguageLoaded(),
  resolveHighlightLanguage() and getLoadedHighlightLanguages() replace
  loadHighlighter() and isHighlighterLoaded().
- Markdown: rehypeLazyHighlight highlights with lowlight's core and the
  languages a message uses, waiting only for one it is the first to use.
  rehype-highlight is no longer in the pipeline, since it bundles
  lowlight's common languages.

First-load JS of the chat page stays at 2.58 MB raw, 0.76 MB gzip,
0.61 MB brotli: no highlight.js code, only the 14 KB language table.
The first code block now loads its language (1-12 KB for most) and the
20 KB core instead of 900 KB.

Assisted-by: Claude (Opus 5.5 Medium)

* ui: name the code-block class names and shell language as constants

Assisted-by: Claude (Opus 5.5 Medium)
2026-11-20 15:06:10 +01:00

251 lines
7.8 KiB
TypeScript

// Microbenchmarks for the functions on the agentic-thread streaming hot path.
//
// Every function here is invoked from a `$derived` that is invalidated on each
// streamed token, for every tool-call section in the message. These numbers give
// the per-call cost; multiply by (tokens x sections) for the real damage.
//
// Run: npx vitest bench --project=unit tests/unit/agentic-hotpath.bench.ts
import { classifyToolResult, parseToolResultWithMedia } from '#lib/utils/agentic.ts';
import {
detectIncompleteCodeBlock,
highlightCode,
loadHighlightLanguages
} from '#lib/utils/code.ts';
import { computeLineDiff } from '#lib/utils/compute-line-diff.ts';
import { preprocessLaTeX } from '#lib/utils/latex-protection.ts';
import { parsePartialJsonArgs } from '#lib/utils/parse-partial-json-args.ts';
import { extractSearchQuery, extractSearchResults } from '#lib/utils/search-results.ts';
import { all as lowlightAll } from 'lowlight';
import rehypeHighlight from 'rehype-highlight';
import rehypeKatex from 'rehype-katex';
import rehypeStringify from 'rehype-stringify';
import { remark } from 'remark';
import remarkBreaks from 'remark-breaks';
import remarkGfm from 'remark-gfm';
import remarkMath from 'remark-math';
import remarkRehype from 'remark-rehype';
import { bench, describe } from 'vitest';
// --- fixtures -------------------------------------------------------------
function lines(n: number, seed: string): string {
const out: string[] = [];
for (let i = 0; i < n; i++) out.push(`${seed} line ${i} const value_${i} = compute(${i});`);
return out.join('\n');
}
const SHELL_OUTPUT_1KB = lines(12, 'out');
const SHELL_OUTPUT_200KB = lines(2600, 'out');
const SHELL_OUTPUT_2MB = lines(26000, 'out');
// Realistic exec_shell_command result: the exit-code marker is the final line,
// which is what the un-anchored EXIT_CODE regex has to scan the whole blob for.
const SHELL_2MB_WITH_EXIT = `${SHELL_OUTPUT_2MB}\n[exit code: 0]`;
const EDIT_OLD_400 = lines(400, 'old');
const EDIT_NEW_400 = lines(400, 'new');
const EDIT_OLD_50 = lines(50, 'old');
const EDIT_NEW_50 = lines(50, 'new');
const WRITE_FILE_ARGS = JSON.stringify({
content: lines(1500, 'src'),
path: '/src/lib/thing.ts'
});
const MARKDOWN_50KB = Array.from(
{ length: 400 },
(_, i) =>
`## Section ${i}\n\nSome **bold** prose with a [link](https://example.com) and \`inline\` code.\n\n- bullet one\n- bullet two\n\n\`\`\`ts\nconst x${i} = ${i};\n\`\`\`\n`
).join('\n');
const CODE_BLOCK_5KB = lines(60, 'code');
// --- A: computeLineDiff (O(m*n) LCS, allocates a full matrix) --------------
describe('computeLineDiff', () => {
bench('50 x 50 lines', () => {
computeLineDiff(EDIT_OLD_50, EDIT_NEW_50);
});
bench('400 x 400 lines', () => {
computeLineDiff(EDIT_OLD_400, EDIT_NEW_400);
});
});
// --- B: the unified processor, rebuilt per call ---------------------------
// Mirrors MarkdownContent.svelte:144-176. The local rehype/remark plugins are
// omitted (they pull in browser-only modules); rehypeHighlight + lowlightAll is
// the dominant term, so this is a lower bound on the real cost.
function buildProcessor() {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
let proc: any = remark().use(remarkGfm);
proc = proc.use(remarkMath).use(remarkBreaks).use(remarkRehype).use(rehypeKatex);
return proc.use(rehypeHighlight, { languages: lowlightAll }).use(rehypeStringify, {
allowDangerousHtml: true
});
}
describe('markdown processor', () => {
bench('build processor (per processMarkdown call, x2)', () => {
buildProcessor();
});
const prebuilt = buildProcessor();
bench('parse 50KB markdown with a prebuilt processor', () => {
prebuilt.parse(MARKDOWN_50KB);
});
});
// Isolates the O(n^2) term measured in Tier 1: processMarkdown re-parses the
// WHOLE accumulated string every frame, while only the last block can have
// changed. If parse() dominates at these sizes, incremental parsing (re-parsing
// only the tail after the last stable block) is the fix; if not, the cost is
// downstream in transform/stringify/DOM.
describe('markdown parse scaling (whole-string reparse per frame)', () => {
const proc = buildProcessor();
const prose = (kb: number) =>
Array.from(
{ length: Math.ceil((kb * 1024) / 64) },
(_, i) => `The quick brown fox jumps over the lazy dog. Sentence ${i}.`
).join(' ');
const MD_3KB = prose(3);
const MD_11KB = prose(11);
const MD_26KB = prose(26);
bench('parse 3KB', () => {
proc.parse(MD_3KB);
});
bench('parse 11KB', () => {
proc.parse(MD_11KB);
});
bench('parse 26KB', () => {
proc.parse(MD_26KB);
});
bench('parse only a 200-char tail (proposed incremental)', () => {
proc.parse(MD_26KB.slice(-200));
});
});
// processMarkdown runs these over the WHOLE accumulated string every frame too,
// before parse() even starts. Measure them before assuming parse is the target.
describe('other whole-string passes per frame', () => {
const prose = (kb: number) =>
Array.from(
{ length: Math.ceil((kb * 1024) / 64) },
(_, i) => `The quick brown fox jumps over the lazy dog. Sentence ${i}.`
).join(' ');
const MD_3KB = prose(3);
const MD_11KB = prose(11);
const MD_26KB = prose(26);
const MD_26KB_LATEX = `${MD_26KB} and some math $x^2 + y^2 = z^2$ inline.`;
bench('preprocessLaTeX 3KB (no latex present)', () => {
preprocessLaTeX(MD_3KB);
});
bench('preprocessLaTeX 11KB (no latex present)', () => {
preprocessLaTeX(MD_11KB);
});
bench('preprocessLaTeX 26KB (no latex present)', () => {
preprocessLaTeX(MD_26KB);
});
bench('preprocessLaTeX 26KB (latex present)', () => {
preprocessLaTeX(MD_26KB_LATEX);
});
bench('detectIncompleteCodeBlock 26KB', () => {
detectIncompleteCodeBlock(MD_26KB);
});
});
// --- C: extractSearchResults, run for EVERY tool of every type ------------
describe('extractSearchResults (only .length > 0 is consumed)', () => {
bench('1KB non-search tool result', () => {
extractSearchResults(SHELL_OUTPUT_1KB);
});
bench('200KB non-search tool result', () => {
extractSearchResults(SHELL_OUTPUT_200KB);
});
bench('2MB non-search tool result', () => {
extractSearchResults(SHELL_OUTPUT_2MB);
});
});
describe('extractSearchQuery', () => {
bench('write_file args (throws on the JSON.parse path)', () => {
extractSearchQuery(WRITE_FILE_ARGS);
});
});
// --- E: the un-anchored exit-code regex -----------------------------------
// Reproduced inline so the bench is independent of the current call site.
const EXIT_CODE_TAIL = /\[exit code: (-?\d+)\]\s*$/;
describe('exit-code regex', () => {
bench('whole 2MB blob (current behaviour)', () => {
SHELL_2MB_WITH_EXIT.match(EXIT_CODE_TAIL);
});
bench('last 64 chars only (proposed)', () => {
SHELL_2MB_WITH_EXIT.slice(-64).match(EXIT_CODE_TAIL);
});
});
// --- per-line result parsers ----------------------------------------------
describe('parseToolResultWithMedia', () => {
bench('1KB', () => {
parseToolResultWithMedia(SHELL_OUTPUT_1KB, []);
});
bench('200KB', () => {
parseToolResultWithMedia(SHELL_OUTPUT_200KB, []);
});
bench('2MB', () => {
parseToolResultWithMedia(SHELL_OUTPUT_2MB, []);
});
});
describe('classifyToolResult', () => {
bench('200KB plain text (falls through to looksLikeMarkdown)', () => {
classifyToolResult(SHELL_OUTPUT_200KB);
});
});
describe('parsePartialJsonArgs', () => {
bench('write_file args, ~60KB (char-by-char scan)', () => {
parsePartialJsonArgs(WRITE_FILE_ARGS);
});
});
// --- F / I: highlight.js ---------------------------------------------------
// highlightCode returns plain text until highlight.js has loaded
await loadHighlightLanguages();
describe('highlightCode', () => {
bench('short bash command (title row, per token)', () => {
highlightCode('grep -rn "foo" src/ | head -50', 'bash');
});
bench('5KB known language', () => {
highlightCode(CODE_BLOCK_5KB, 'typescript');
});
bench('5KB unknown language -> highlightAuto', () => {
highlightCode(CODE_BLOCK_5KB, 'not-a-language');
});
});