Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions cpp/web/host.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<Work> &q = !worker_q.empty() ? worker_q : main_q;
Work w = q.front();
q.pop_front();
Expand Down
13 changes: 8 additions & 5 deletions scripts/bench-web.mjs
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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' };

Expand Down Expand Up @@ -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`);
Expand All @@ -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}`);
Expand Down
39 changes: 30 additions & 9 deletions src/web/EditorView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -561,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(
Expand Down Expand Up @@ -669,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 || !this.attached.has(line)) return null;
const box = this.root.getBoundingClientRect();
if (!entry || entry.stale || !this.attached.has(line)) return null;
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 };
}
Expand Down Expand Up @@ -702,8 +705,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;
Expand Down Expand Up @@ -896,7 +908,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);
Expand Down Expand Up @@ -1158,11 +1170,20 @@ 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]);
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();
Expand All @@ -1176,7 +1197,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;
Expand Down
34 changes: 34 additions & 0 deletions src/web/__tests__/pump.test.ts
Original file line number Diff line number Diff line change
@@ -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);
});
4 changes: 2 additions & 2 deletions src/web/__tests__/vectors.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/web/core/EditorCore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'];
Expand Down
3 changes: 2 additions & 1 deletion src/web/core/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
7 changes: 5 additions & 2 deletions src/web/pump.ts
Original file line number Diff line number Diff line change
@@ -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 };
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion tests/web/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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++);
}
45 changes: 45 additions & 0 deletions tests/web/render.e2e.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,51 @@ 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, 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,
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();
});

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 () => {
Expand Down
Loading