From 4d58551b08cc4eb7028ccd6bcf31555cf0594951 Mon Sep 17 00:00:00 2001 From: Rayen Sbai Date: Tue, 17 Nov 2026 18:18:27 +0100 Subject: [PATCH 1/6] perf(web): start an engine step only when a parse slice still fits the frame The page pump started another queued item until its 6 ms ran out, so a second 4 ms parse slice could start with 2 ms left, and tree-sitter overshoots a slice during error recovery. Typing at the start of a line in a 10k-line file made engine tasks of 16-18 ms on a phone-speed CPU. The pump now starts an item only while a whole slice fits, and a slice is 3 ms, so the longest task is about 12 ms. --- cpp/web/host.cpp | 9 +++++--- src/web/__tests__/pump.test.ts | 34 +++++++++++++++++++++++++++++++ src/web/__tests__/vectors.test.ts | 4 ++-- src/web/core/EditorCore.ts | 2 +- src/web/core/module.ts | 3 ++- src/web/pump.ts | 7 +++++-- tests/web/core.ts | 2 +- 7 files changed, 51 insertions(+), 10 deletions(-) create mode 100644 src/web/__tests__/pump.test.ts diff --git a/cpp/web/host.cpp b/cpp/web/host.cpp index 164a7fa..e31c383 100644 --- a/cpp/web/host.cpp +++ b/cpp/web/host.cpp @@ -238,10 +238,13 @@ CPW void cpw_set_clock(int32_t e, double millis) { if (Editor *ed = at(e)) ed->clock = millis; } -// Runs worker work, then main work, until both queues are empty or budget_ms is spent. Returns the items left. -CPW int32_t cpw_pump(double budget_ms) { +// Runs worker work, then main work, until both queues are empty or budget_ms is spent. After the first item, another +// starts only while a parse slice of slice_ms still fits. Returns the items left. +CPW int32_t cpw_pump(double budget_ms, double slice_ms) { double end = emscripten_get_now() + budget_ms; - while ((!worker_q.empty() || !main_q.empty()) && emscripten_get_now() < end) { + bool first = true; + while ((!worker_q.empty() || !main_q.empty()) && (first || emscripten_get_now() + slice_ms < end)) { + first = false; std::deque &q = !worker_q.empty() ? worker_q : main_q; Work w = q.front(); q.pop_front(); diff --git a/src/web/__tests__/pump.test.ts b/src/web/__tests__/pump.test.ts new file mode 100644 index 0000000..08b155f --- /dev/null +++ b/src/web/__tests__/pump.test.ts @@ -0,0 +1,34 @@ +import { existsSync } from 'node:fs'; +import { join } from 'node:path'; +import { expect, test } from '@jest/globals'; +import { generateJavaScript } from '../../../example/src/bench/generate'; +import { builtCore, loadCore, loadGrammar, pumpAll, ROOT } from '../../../tests/web/core'; +import { EditorCore } from '../core/EditorCore'; + +const dir = builtCore('lib/web', 'yarn build:web'); +const javascript = join(ROOT, 'languages/javascript/web/javascript.wasm'); +const ready = dir !== null && existsSync(javascript); +if (dir && !ready) console.warn('javascript grammar not built, run yarn build:web-grammars'); + +// The jest preset rounds performance.now to whole milliseconds. +const now = () => Number(process.hrtime.bigint()) / 1e6; + +(ready ? test : test.skip)('one pump starts no second parse slice', async () => { + const m = await loadCore(dir as string); + loadGrammar(m, javascript, 'javascript', join(ROOT, 'languages/javascript/queries/javascript')); + const core = new EditorCore(m, generateJavaScript(100_000), 4000); + expect(core.setLanguage('javascript')).toBe(true); + const took: number[] = []; + for (let i = 0; i < 30; i++) { + const start = now(); + const left = m._cpw_pump(6, 4); + took.push(now() - start); + if (left === 0) break; + } + expect(took.length).toBe(30); + took.sort((a, b) => a - b); + // One 4 ms slice a call; two slices take about 8. + expect(took[15]).toBeLessThan(5.5); + core.close(); + pumpAll(m); +}); diff --git a/src/web/__tests__/vectors.test.ts b/src/web/__tests__/vectors.test.ts index 1d76b71..b1b5fb4 100644 --- a/src/web/__tests__/vectors.test.ts +++ b/src/web/__tests__/vectors.test.ts @@ -424,8 +424,8 @@ if (dir && !existsSync(javascript)) pumpAll(m); expect(core.snapshot).not.toBe(0); core.setText(js10k); - m._cpw_pump(1); - expect(m._cpw_pump(0)).toBeGreaterThan(0); + m._cpw_pump(1, 0); + expect(m._cpw_pump(0, 0)).toBeGreaterThan(0); core.close(); pumpAll(m); expect(m._cpw_test_held_snapshots()).toBe(0); diff --git a/src/web/core/EditorCore.ts b/src/web/core/EditorCore.ts index 5a8c4c3..111c771 100644 --- a/src/web/core/EditorCore.ts +++ b/src/web/core/EditorCore.ts @@ -31,7 +31,7 @@ export type WebChange = { export class SearchError extends Error {} // The engine's parse budget per slice, so the page's pump stays inside a frame. -export const DEFAULT_BUDGET_MICROS = 4000; +export const DEFAULT_BUDGET_MICROS = 3000; const ORIGINS: ChangeOrigin[] = ['input', 'api', 'undo', 'redo']; const MOVES: MoveUnit[] = ['char', 'word', 'lineStart', 'lineEnd', 'docStart', 'docEnd']; diff --git a/src/web/core/module.ts b/src/web/core/module.ts index dcf93c7..e4c2afc 100644 --- a/src/web/core/module.ts +++ b/src/web/core/module.ts @@ -36,7 +36,8 @@ export interface CoreModule { _cpw_take_missed(e: number, out: Ptr, cap: number): number; _cpw_refresh(e: number): void; _cpw_set_clock(e: number, millis: number): void; - _cpw_pump(budgetMs: number): number; + // Starts another queued item only while sliceMs still fits before budgetMs runs out. + _cpw_pump(budgetMs: number, sliceMs: number): number; _cpw_take_changes( e: number, out: Ptr, diff --git a/src/web/pump.ts b/src/web/pump.ts index d99296c..82bfd4f 100644 --- a/src/web/pump.ts +++ b/src/web/pump.ts @@ -1,7 +1,10 @@ +import { DEFAULT_BUDGET_MICROS } from './core/EditorCore'; import type { CoreModule } from './core/module'; // The engine's share of a task, so a key still draws soon after it. export const PUMP_BUDGET_MS = 6; +// An engine step can run one parse slice, so the pump starts one only while a slice still fits. +const PARSE_SLICE_MS = DEFAULT_BUDGET_MICROS / 1000; // urgent: the view's next frame waits for the parse. pending: it has something to draw. type View = { draw(): void; readonly urgent: boolean; pending(): boolean }; @@ -68,7 +71,7 @@ function frame(): void { scheduled = false; const start = probe ? performance.now() : 0; const urgent = [...views].some((v) => v.urgent); - const left = core && urgent ? core._cpw_pump(PUMP_BUDGET_MS) : 0; + const left = core && urgent ? core._cpw_pump(PUMP_BUDGET_MS, PARSE_SLICE_MS) : 0; const pumped = probe ? performance.now() - start : 0; for (const view of [...views]) { if (probe && view === keyView) drawKeyed(probe, view, pumped); @@ -111,7 +114,7 @@ function runEngine(): void { engineQueued = false; if (!core) return; const start = probe ? performance.now() : 0; - const left = core._cpw_pump(PUMP_BUDGET_MS); + const left = core._cpw_pump(PUMP_BUDGET_MS, PARSE_SLICE_MS); if (probe) probe.tasks.push(performance.now() - start); if ([...views].some((v) => v.pending())) requestPump(); if (left > 0) postEngine(); diff --git a/tests/web/core.ts b/tests/web/core.ts index 57c6952..5486057 100644 --- a/tests/web/core.ts +++ b/tests/web/core.ts @@ -125,5 +125,5 @@ export function highlight( } export function pumpAll(m: RawCore, budgetMs = 8): void { - for (let i = 0; i < 100000 && m._cpw_pump(budgetMs) > 0; i++); + for (let i = 0; i < 100000 && m._cpw_pump(budgetMs, 0) > 0; i++); } From 8cb7da0cc772d664e30766c636a9b4a531ef8a24 Mon Sep 17 00:00:00 2001 From: Rayen Sbai Date: Tue, 17 Nov 2026 18:25:05 +0100 Subject: [PATCH 2/6] chore(web): a key count and the mean in the typing bench --- scripts/bench-web.mjs | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/scripts/bench-web.mjs b/scripts/bench-web.mjs index 4a8bacb..d9b3a35 100644 --- a/scripts/bench-web.mjs +++ b/scripts/bench-web.mjs @@ -1,4 +1,4 @@ -// Typing bench in Chrome: yarn bench:web [lines] [port] [--desktop [rate]] [--profile] +// Typing bench in Chrome: yarn bench:web [lines] [port] [--desktop [rate]] [--profile] [--keys n] // Without --desktop it drives Chrome on an Android device through adb, the example served on that port with adb reverse. // --desktop opens the example in a local Chromium at phone size with the CPU slowed by rate. --profile adds a CPU profile. import { execFileSync } from 'node:child_process'; @@ -14,17 +14,19 @@ const args = process.argv.slice(2); const positional = []; let desktop = null; let profile = false; +let keys = 60; for (let i = 0; i < args.length; i++) { if (args[i] === '--profile') profile = true; + else if (args[i] === '--keys') keys = Number(args[++i]); else if (args[i] === '--desktop') { desktop = /^\d+(\.\d+)?$/.test(args[i + 1] ?? '') ? Number(args[++i]) : DEFAULT_RATE; } else positional.push(args[i]); } const lines = Number(positional[0] ?? 300); const WEB_PORT = positional[1] ?? '8082'; -const CHARS = 60; +const CHARS = keys; const GAP_MS = 150; -const TEXT = 'let total = value + 1; '.repeat(3).slice(0, CHARS); +const TEXT = 'let total = value + 1; '.repeat(Math.ceil(CHARS / 23)).slice(0, CHARS); const PORT = 9222; const ROOTS = { drawKeyed: 'key draw', runEngine: 'engine', applyInput: 'handler' }; @@ -92,7 +94,8 @@ try { const probe = await page.evaluate(() => globalThis.__codepadProbe); const stats = (list) => { const sorted = [...list].sort((a, b) => a - b); - return `p50 ${ms(pct(sorted, 0.5))}, p95 ${ms(pct(sorted, 0.95))}, max ${ms(sorted.at(-1) ?? 0)}`; + const mean = list.reduce((a, b) => a + b, 0) / Math.max(1, list.length); + return `mean ${ms(mean)}, p50 ${ms(pct(sorted, 0.5))}, p95 ${ms(pct(sorted, 0.95))}, max ${ms(sorted.at(-1) ?? 0)}`; }; const where = desktop !== null ? `desktop at ${desktop}x` : 'device'; console.log(`${lines} lines, ${where}, ${probe.work.length} of ${CHARS} keys`); @@ -110,7 +113,7 @@ try { const top = summarize(taken, ROOTS, 12); for (const group of ['key draw', 'handler', 'engine', 'other']) { const total = (all[group] ?? []).reduce((sum, f) => sum + f.selfMs, 0); - console.log(` profile ${group}: ${ms(total)} over the 60 keys`); + console.log(` profile ${group}: ${ms(total)} over the ${CHARS} keys`); for (const f of top[group] ?? []) console.log(` ${ms(f.selfMs).padStart(9)} ${f.fn}`); } console.log(` profile saved to ${file}`); From 26e5cbc6c93958a8255b53ad0fb4f88498581e9b Mon Sep 17 00:00:00 2001 From: Rayen Sbai Date: Tue, 17 Nov 2026 18:25:06 +0100 Subject: [PATCH 3/6] perf(web): keep a line's element when a key edits only that line Each key removed the edited line's element and built a new one in the next frame, which cost about 0.4 ms of the key's handler on a phone-speed CPU. The element stays, marked stale, and the frame repaints it in place. --- src/web/EditorView.ts | 13 +++++++++---- tests/web/render.e2e.mjs | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 4 deletions(-) diff --git a/src/web/EditorView.ts b/src/web/EditorView.ts index c899553..219afb5 100644 --- a/src/web/EditorView.ts +++ b/src/web/EditorView.ts @@ -89,6 +89,8 @@ type Entry = { revision: number; spans: Int32Array | null; text: string; + // Edited since its last draw: the element is reused, but the frame reads the line again. + stale?: boolean; }; // The edited line's previous spans moved along an edit that stayed inside it, kept until a parse lands. @@ -673,7 +675,7 @@ export class EditorView { if (this.destroyed) return null; const line = this.core.lineAt(offset); const entry = this.cache.get(line); - if (!entry || !this.attached.has(line)) return null; + if (!entry || entry.stale || !this.attached.has(line)) return null; const box = this.root.getBoundingClientRect(); const edge = caretEdge(entry.el, offset - this.core.lineStart(line)); return { x: edge.x - box.left, y: edge.top - box.top, width: 1, height: this.rowHeight }; @@ -896,7 +898,7 @@ export class EditorView { const info = snapshot ? this.snapshotInfo(snapshot) : null; const incremental = info !== null && info.basedOn >= 0 && info.basedOn === this.appliedRevision; const pairs: number[] = []; - for (const [line, entry] of this.cache) pairs.push(line, entry.revision); + for (const [line, entry] of this.cache) if (!entry.stale) pairs.push(line, entry.revision); const entries = m._malloc(Math.max(1, pairs.length) * 4); m.HEAP32.set(pairs, entries >> 2); const lenPtr = m._malloc(4); @@ -1162,7 +1164,10 @@ export class EditorView { for (const [line, entry] of this.cache) { if (line < first) moved.push([line, entry]); else if (line >= first + oldCount) moved.push([line + delta, entry]); - else entry.el.remove(); + else if (oldCount === 1 && newCount === 1) { + entry.stale = true; + moved.push([line, entry]); + } else entry.el.remove(); } this.cache.clear(); this.attached.clear(); @@ -1176,7 +1181,7 @@ export class EditorView { // The spans a line shows now: its cache entry, else what an edit since the last frame kept for it. private shownSpans(line: number): Int32Array | null { const entry = this.cache.get(line); - if (entry) return entry.spans; + if (entry && !entry.stale) return entry.spans; const kept = this.kept; if (!kept) return null; if (line === kept.line) return kept.spans; diff --git a/tests/web/render.e2e.mjs b/tests/web/render.e2e.mjs index 795fe4f..52cdfef 100644 --- a/tests/web/render.e2e.mjs +++ b/tests/web/render.e2e.mjs @@ -430,6 +430,45 @@ for (const engine of [chromium, webkit]) { await page.close(); }); + test("a typed letter keeps its line's element", async () => { + const { page } = await open(); + await page.evaluate(() => window.codepad.ensureLanguage('python')); + await editor(page, 'a', 'def a():\n return b\n'); + await page.evaluate(async () => { + window.views.a.setLanguage('python'); + await window.codepad.until(() => window.views.a.core.snapshot !== 0); + }); + await clickAt(page, 'a', 1, 12); + await page.evaluate(() => { + window.kept = document.querySelector('#a [data-line="1"]'); + }); + await page.keyboard.type('x'); + const same = await page.evaluate(async () => { + const v = window.views.a; + await window.codepad.until(() => v.core.snapshot !== 0 && v.core.revision > 0); + await window.codepad.frames(3); + return document.querySelector('#a [data-line="1"]') === window.kept; + }); + assert.equal(await textOf(page, 'a'), 'def a():\n return bx\n'); + assert.equal(same, true); + await page.close(); + }); + + test('typing past the right edge keeps the caret in view without soft wrap', async () => { + const { page } = await open(); + await editor(page, 'a', 'x'); + await clickAt(page, 'a', 0, 1); + await page.keyboard.type('a'.repeat(90)); + const { x, width } = await page.evaluate(async () => { + const v = window.views.a; + await window.codepad.frames(2); + const caret = v.caretRect(v.core.selection.head); + return { x: caret?.x ?? -1, width: document.getElementById('a').clientWidth }; + }); + assert.ok(x >= 0 && x <= width, `caret x ${x} of ${width}`); + await page.close(); + }); + test('the frame after a letter, an Enter and a joining Backspace keeps the keyword colors', async () => { const { page } = await open(); await page.evaluate(async () => { From 54a4deb348c85438097e877226113ffdda0d9483 Mon Sep 17 00:00:00 2001 From: Rayen Sbai Date: Tue, 17 Nov 2026 18:27:42 +0100 Subject: [PATCH 4/6] perf(web): skip moving the line cache when an edit stays inside one line --- src/web/EditorView.ts | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/web/EditorView.ts b/src/web/EditorView.ts index 219afb5..ce5e0a3 100644 --- a/src/web/EditorView.ts +++ b/src/web/EditorView.ts @@ -1160,6 +1160,12 @@ export class EditorView { this.keepSpans(first, oldCount, newCount, change); if (colored && this.kept === null) this.urgent = true; const delta = newCount - oldCount; + // An edit inside one line moves no other line. + if (oldCount === 1 && newCount === 1) { + const entry = this.cache.get(first); + if (entry) entry.stale = true; + return; + } const moved: [number, Entry][] = []; for (const [line, entry] of this.cache) { if (line < first) moved.push([line, entry]); From 89f0ebf77b61202ca12d3498eb2c14641db1fb9b Mon Sep 17 00:00:00 2001 From: Rayen Sbai Date: Tue, 17 Nov 2026 18:30:36 +0100 Subject: [PATCH 5/6] perf(web): follow a horizontal caret reveal without drawing the frame again Typing past the right edge drew every visible line twice in that key's frame, once to find the caret and once after the scroll, which was about a third of the keys on a phone-width editor. Lines and the caret move with the scrolled content, so only the textarea is placed again. --- src/web/EditorView.ts | 9 +++++++++ tests/web/render.e2e.mjs | 10 ++++++++-- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/src/web/EditorView.ts b/src/web/EditorView.ts index ce5e0a3..5e917c5 100644 --- a/src/web/EditorView.ts +++ b/src/web/EditorView.ts @@ -704,8 +704,17 @@ export class EditorView { // A caret moved off screen scrolls into view before the paint: by row while its line is not drawn, then by x. for (let pass = 0; this.revealing && pass < 2; pass++) { if (this.drawnCaret) this.revealing = false; + const [beforeX, beforeY] = [this.layout.scrollX, this.layout.scrollY]; if (!this.revealCaret()) break; this.syncScroll(); + const caret = this.drawnCaret; + if (caret && this.layout.scrollY === beforeY) { + // Lines and the caret scroll with the content: only the textarea, placed in view coordinates, follows. + const x = caret.x - (this.layout.scrollX - beforeX); + this.drawnCaret = { ...caret, x }; + if (this.input.focused) this.input.place(x, caret.y); + break; + } moved = this.frame(); } this.revealing = false; diff --git a/tests/web/render.e2e.mjs b/tests/web/render.e2e.mjs index 52cdfef..5e361c1 100644 --- a/tests/web/render.e2e.mjs +++ b/tests/web/render.e2e.mjs @@ -459,13 +459,19 @@ for (const engine of [chromium, webkit]) { await editor(page, 'a', 'x'); await clickAt(page, 'a', 0, 1); await page.keyboard.type('a'.repeat(90)); - const { x, width } = await page.evaluate(async () => { + const { x, width, textarea } = await page.evaluate(async () => { const v = window.views.a; await window.codepad.frames(2); const caret = v.caretRect(v.core.selection.head); - return { x: caret?.x ?? -1, width: document.getElementById('a').clientWidth }; + return { + x: caret?.x ?? -1, + width: document.getElementById('a').clientWidth, + textarea: Number.parseFloat(document.querySelector('#a textarea').style.left), + }; }); assert.ok(x >= 0 && x <= width, `caret x ${x} of ${width}`); + // The keyboard's candidate window opens at the textarea. + assert.ok(Math.abs(textarea - x) <= 1, `textarea at ${textarea}, caret at ${x}`); await page.close(); }); From a5e4bffa1bb66d05534a7172fee79d2cdba8c4c1 Mon Sep 17 00:00:00 2001 From: Rayen Sbai Date: Tue, 17 Nov 2026 18:33:02 +0100 Subject: [PATCH 6/6] perf(web): read the editor's box once per draw and the caret row only with wrap --- src/web/EditorView.ts | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/web/EditorView.ts b/src/web/EditorView.ts index 5e917c5..f1b2bfd 100644 --- a/src/web/EditorView.ts +++ b/src/web/EditorView.ts @@ -563,12 +563,13 @@ export class EditorView { private drawInput(): void { const box = this.textLayer.getBoundingClientRect(); const { anchor, head } = this.core.selection; - const caret = this.caretRect(head); - this.drawnCaret = caret; const rootBox = this.root.getBoundingClientRect(); + const caret = this.caretRect(head, rootBox); + this.drawnCaret = caret; const caretLine = this.cache.get(this.core.lineAt(head))?.el; + // Without soft wrap every line is one row. this.drawnCaretRow = - caret && caretLine + this.o.softWrap && caret && caretLine ? Math.max( 0, Math.round( @@ -671,12 +672,12 @@ export class EditorView { } // The caret at offset in view coordinates, or null when its line is not drawn. - caretRect(offset: number): Rect | null { + caretRect(offset: number, rootBox?: DOMRect): Rect | null { if (this.destroyed) return null; const line = this.core.lineAt(offset); const entry = this.cache.get(line); if (!entry || entry.stale || !this.attached.has(line)) return null; - const box = this.root.getBoundingClientRect(); + const box = rootBox ?? this.root.getBoundingClientRect(); const edge = caretEdge(entry.el, offset - this.core.lineStart(line)); return { x: edge.x - box.left, y: edge.top - box.top, width: 1, height: this.rowHeight }; }