perf(editor): a long note's first paint is the viewport; the rest at idle (plan 1c) #1159

Closed
kayg wants to merge 0 commits from perf/editor-first-paint-1c into dev
Owner

Plan docs/plans/notes-float-port.md (on port/notes-float-shell): "1c. The editor's first paint" and "Measured switch and open-note costs" item 5. Opening a long note now draws the title and the blocks that fit (CodeMirror's viewport and its margin) first, and decorates the rest at idle. The caret and keys work in frame 0.

What cost the time (dev 245875408, Chromium on dev-01, 4× CPU throttle, 5k-line note with every block kind)

The editor already virtualises lines. What it didn't do was stop at the viewport for the syntax tree: three state-built parts forced a parse of the whole note before the first frame.

  • embeds (embed-lines.ts, inCode): about 1/3 of open CPU. Every ![ line called ensureSyntaxTree up to itself.
  • code colours (code/blocks.ts): about 1/4. Every code block in the note was painted again on each syntax tree change.
  • tables (tables.ts): a forced whole-note parse with a 25 ms budget, then every table drawn.
  • the top pills' heading tracker (headings.ts): a forced whole-note parse, one frame later.

The change

  • syntax/parse-ahead.ts (new, in the editor core):
    • Before a frame paints, the state's tree covers the drawn lines. The hidden-syntax pass already parses that far; a microtask hands the tree to the state, so the state-built parts see it in the same frame and nothing pops in.
    • The rest of the note is parsed at idle in 10k-character slices, or a 90 ms timer on Safari, which has no requestIdleCallback.
    • Idle parsing waits 400 ms after a key or any scroll, and 1 s after a note is shown or goes live.
  • Embeds, code colours and tables draw as far as the tree reaches and follow it as it grows: only the newly reached part is looked at, never everything again.
  • syntax/tree-to.ts (new): after an edit, a part asks the tree to reach where it reached before (25 ms at most, the table finder's old budget). A re-parse cut short on a busy machine can then no longer take down a grid you are typing in.
  • A table is only ever drawn whole: one cut where the parsed tree stops waits for the rest of its rows.
  • The page's heading pills read the headings parsed so far, headingsOf(state, false), and read again when parsedTo moves. The Outline tab still reads all headings (it lists every one).

Rules kept:

  • No layer between edits and the note doc. Every key edits the note's document in its own transaction. Only when a part is decorated changes; nothing holds a copy of any text.
  • Find, select-all, undo and scroll-to-anchor (?line=) work on the document, so they reach blocks that are not drawn yet. The new e2e specs cover a jump to the end, a far ?line=, undo and select-all.
  • No feature dropped. No machinery in the UI.

Decisions (made, not asked)

  • "Small overscan" = CodeMirror's own viewport margin (1000 px). It isn't configurable, so I kept it rather than patching the engine.
  • Idle parsing goes to the end of the note, so a later jump to the end or a far link costs nothing. CodeMirror's own worker stops 100k characters ahead of the viewport.
  • Outline tab keeps its whole-note read. It is user-opened and lists every heading; partial would mean rows popping in.

Gates

Results are posted as PR comments.

🤖 Generated with Claude Code

Plan `docs/plans/notes-float-port.md` (on `port/notes-float-shell`): "1c. The editor's first paint" and "Measured switch and open-note costs" item 5. Opening a long note now draws the title and the blocks that fit (CodeMirror's viewport and its margin) first, and decorates the rest at idle. The caret and keys work in frame 0. ## What cost the time (dev 245875408, Chromium on dev-01, 4× CPU throttle, 5k-line note with every block kind) The editor already virtualises lines. What it didn't do was stop at the viewport for the **syntax tree**: three state-built parts forced a parse of the whole note before the first frame. - embeds (`embed-lines.ts`, `inCode`): about 1/3 of open CPU. Every `![` line called `ensureSyntaxTree` up to itself. - code colours (`code/blocks.ts`): about 1/4. Every code block in the note was painted again on each syntax tree change. - tables (`tables.ts`): a forced whole-note parse with a 25 ms budget, then every table drawn. - the top pills' heading tracker (`headings.ts`): a forced whole-note parse, one frame later. ## The change - `syntax/parse-ahead.ts` (new, in the editor core): - Before a frame paints, the state's tree covers the drawn lines. The hidden-syntax pass already parses that far; a microtask hands the tree to the state, so the state-built parts see it in the same frame and nothing pops in. - The rest of the note is parsed at idle in 10k-character slices, or a 90 ms timer on Safari, which has no `requestIdleCallback`. - Idle parsing waits 400 ms after a key or any scroll, and 1 s after a note is shown or goes live. - Embeds, code colours and tables draw as far as the tree reaches and follow it as it grows: only the newly reached part is looked at, never everything again. - `syntax/tree-to.ts` (new): after an edit, a part asks the tree to reach where it reached before (25 ms at most, the table finder's old budget). A re-parse cut short on a busy machine can then no longer take down a grid you are typing in. - A table is only ever drawn whole: one cut where the parsed tree stops waits for the rest of its rows. - The page's heading pills read the headings parsed so far, `headingsOf(state, false)`, and read again when `parsedTo` moves. The Outline tab still reads all headings (it lists every one). Rules kept: - **No layer between edits and the note doc.** Every key edits the note's document in its own transaction. Only *when* a part is decorated changes; nothing holds a copy of any text. - Find, select-all, undo and scroll-to-anchor (`?line=`) work on the document, so they reach blocks that are not drawn yet. The new e2e specs cover a jump to the end, a far `?line=`, undo and select-all. - No feature dropped. No machinery in the UI. ## Decisions (made, not asked) - **"Small overscan" = CodeMirror's own viewport margin** (1000 px). It isn't configurable, so I kept it rather than patching the engine. - **Idle parsing goes to the end of the note**, so a later jump to the end or a far link costs nothing. CodeMirror's own worker stops 100k characters ahead of the viewport. - **Outline tab keeps its whole-note read.** It is user-opened and lists every heading; partial would mean rows popping in. ## Gates Results are posted as PR comments. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
kayg added 11 commits 2026-11-15 09:52:52 +00:00
Opening a long note no longer parses all of its markdown before the first
frame. The embed, table and code block parts draw as far as the syntax tree
reaches and follow it as it grows; a new parse-ahead plugin hands the tree
for the drawn lines to the state before the frame paints (no pop-in) and
parses the rest at idle in small slices, pausing while typing or scrolling.
The page's heading pills read the parsed headings only and read again as
the tree grows. The text is never touched or copied.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Safari has no requestIdleCallback; its stand-in timer must not land in the
open's own frames.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
On a busy machine the language field's re-parse after a key can stop short;
tables, embeds and code colours drawn from that shorter tree took down a
grid while typing in it. They now ask the tree to reach as far as it did
before the edit (25 ms at most, the table finder's old budget), never more
than was already parsed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
perf(budgets): record the 1c sizes (index +0.52 KB first paint, lazy editor chunks)
Some checks failed
CI / server (pull_request) Has been cancelled
CI / rust (pull_request) Has been cancelled
CI / web (pull_request) Has been cancelled
Markdown extraction contract / markdown-native-contract (pull_request) Has been cancelled
Markdown extraction contract / markdown-editor-contract (pull_request) Has been cancelled
6037b68ef8
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Author
Owner

Gates at 6037b68ef (product code = 137bc8a51; later commits are tests, budgets, harness)

gate where / command result
editor typecheck dev-01 cd packages/editor && bun run check ok
editor vitest dev-01 bunx vitest run 671 passed (incl. new first-paint-1c.test.ts, 7)
web vitest dev-01 apps/web: bunx vitest run 319 passed
svelte-check dev-01 bunx svelte-check --threshold error 0 errors (19 warnings, as on dev)
e2e chromium-1440 dev-01 CALTERNAL_BIN=… bunx playwright test <18 specs> --project=chromium-1440 --workers=1 116 passed
e2e chromium-390 same, --project=chromium-390 106 passed
e2e webkit-1440 dev-02, same specs 112 passed
e2e webkit-390 dev-02 101 passed
repeat tables.spec.ts:112, tables.spec.ts:273, long-note-first-paint.spec.ts --repeat-each=10 on all 4 projects 40/40 each
bundle PERF_DIST=… perf/web.sh --bundle-only --gate pass with the new lines (index 232.00 → 232.518 KB first paint, +0.52; see _editor_1c in budgets.json)

Specs: long-note-first-paint (new), editor, embeds, note-embeds, tables, tables-more, tables-parity, code-block, vim, vim-block, links, keyboard, note-pills, title-pill, switching, app-switch, search-palette, list-search.

The first gate round had one table failure per engine (tables:273 chromium, tables:112 webkit). Both passed 5/5 standalone on branch and dev. The cause was a real risk in the branch: after a key, the language field's re-parse can stop short on a busy machine, and the table field then drew from the cut tree. Fixed in 137bc8a51 (tree-to.ts). A second round failed with ENOENT on trace files: my own two runs shared one test-results dir. Rerun with --output per project, all green above.

Chromium breakdown (dev-01, 4× throttle, 3 cycles, ABAB dev/1c/1c/dev, medians, ms)

Not a gate (dev-01 is not a perf VM); this is the "where the time goes" step.

note build text shown live script in open idle tail script (2 s after)
rich 1k lines dev 224 840 175 95
1c 162 592 191 271
rich 5k lines dev 490 1073 144 110
1c 137 595 182 764
prose 4k lines (no embeds/tables) dev 97 353 138 168
1c 95 326 150 584

The parse and paint work moved from the open into idle slices (the tail). For notes with no embeds or tables, dev never parsed past the viewport plus 100k characters; 1c parses to the end at idle, which is new idle work. Profiles (inclusive, unminified, 3 opens): dev 5k inCode→ensureSyntaxTree 1001, everything (code) 577, tables activate 461; 1c none of these on the open path.

Real Safari, macos-01 (8 cycles ABAB, one session per run, blank page at ~60 rAF/s first, idle baseline)

Median per run; worst = longest rAF gap within 2.5 s of the open; shown/live in ms.

run note dev worst 1c worst dev shown 1c shown dev live 1c live
1 1k 102 226 59 65 162 292
1 5k 92 96 76 50 153 144
2 1k 181 171 103 86 216 282
2 5k 124 169 107 64 193 215
3 1k 145 125 68 69 195 175
3 5k 88 100 71 58 147 154

Idle baselines matched (worst 24–29 ms in both builds). The short-note control also swung widely in run 2 (dev 248, 1c 544), so these frames are noisy. Run 1 and run 2 1c used builds from before 137bc8a51/acdd7ad74; run 3 is the final code.

An in-page probe (EditorView update/measure plus setTimeout/rAF callbacks timed) found the 100–400 ms open frames in both builds are browser rendering, not editor JS: no update over 27 ms. The one idle slice seen took 17 ms at +2.2 s; dev has a 36 ms frame at the same moment.

Reading: on Safari the 5k text is shown sooner in every run. Worst frames are flat within this VM's noise, and 1k "live" is mixed (+130, +66, −20).

## Gates at 6037b68ef (product code = 137bc8a51; later commits are tests, budgets, harness) | gate | where / command | result | |---|---|---| | editor typecheck | dev-01 `cd packages/editor && bun run check` | ok | | editor vitest | dev-01 `bunx vitest run` | 671 passed (incl. new `first-paint-1c.test.ts`, 7) | | web vitest | dev-01 `apps/web: bunx vitest run` | 319 passed | | svelte-check | dev-01 `bunx svelte-check --threshold error` | 0 errors (19 warnings, as on dev) | | e2e chromium-1440 | dev-01 `CALTERNAL_BIN=… bunx playwright test <18 specs> --project=chromium-1440 --workers=1` | 116 passed | | e2e chromium-390 | same, `--project=chromium-390` | 106 passed | | e2e webkit-1440 | dev-02, same specs | 112 passed | | e2e webkit-390 | dev-02 | 101 passed | | repeat | `tables.spec.ts:112`, `tables.spec.ts:273`, `long-note-first-paint.spec.ts` `--repeat-each=10` on all 4 projects | 40/40 each | | bundle | `PERF_DIST=… perf/web.sh --bundle-only --gate` | pass with the new lines (index 232.00 → 232.518 KB first paint, +0.52; see `_editor_1c` in budgets.json) | Specs: long-note-first-paint (new), editor, embeds, note-embeds, tables, tables-more, tables-parity, code-block, vim, vim-block, links, keyboard, note-pills, title-pill, switching, app-switch, search-palette, list-search. The first gate round had one table failure per engine (tables:273 chromium, tables:112 webkit). Both passed 5/5 standalone on branch and dev. The cause was a real risk in the branch: after a key, the language field's re-parse can stop short on a busy machine, and the table field then drew from the cut tree. Fixed in 137bc8a51 (`tree-to.ts`). A second round failed with ENOENT on trace files: my own two runs shared one `test-results` dir. Rerun with `--output` per project, all green above. ## Chromium breakdown (dev-01, 4× throttle, 3 cycles, ABAB dev/1c/1c/dev, medians, ms) Not a gate (dev-01 is not a perf VM); this is the "where the time goes" step. | note | build | text shown | live | script in open | idle tail script (2 s after) | |---|---|---|---|---|---| | rich 1k lines | dev | 224 | 840 | 175 | 95 | | | 1c | 162 | 592 | 191 | 271 | | rich 5k lines | dev | 490 | 1073 | 144 | 110 | | | 1c | 137 | 595 | 182 | 764 | | prose 4k lines (no embeds/tables) | dev | 97 | 353 | 138 | 168 | | | 1c | 95 | 326 | 150 | 584 | The parse and paint work moved from the open into idle slices (the tail). For notes with no embeds or tables, dev never parsed past the viewport plus 100k characters; 1c parses to the end at idle, which is new idle work. Profiles (inclusive, unminified, 3 opens): dev 5k `inCode→ensureSyntaxTree` 1001, `everything` (code) 577, tables `activate` 461; 1c none of these on the open path. ## Real Safari, macos-01 (8 cycles ABAB, one session per run, blank page at ~60 rAF/s first, idle baseline) Median per run; worst = longest rAF gap within 2.5 s of the open; shown/live in ms. | run | note | dev worst | 1c worst | dev shown | 1c shown | dev live | 1c live | |---|---|---|---|---|---|---|---| | 1 | 1k | 102 | 226 | 59 | 65 | 162 | 292 | | 1 | 5k | 92 | 96 | 76 | 50 | 153 | 144 | | 2 | 1k | 181 | 171 | 103 | 86 | 216 | 282 | | 2 | 5k | 124 | 169 | 107 | 64 | 193 | 215 | | 3 | 1k | 145 | 125 | 68 | 69 | 195 | 175 | | 3 | 5k | 88 | 100 | 71 | 58 | 147 | 154 | Idle baselines matched (worst 24–29 ms in both builds). The short-note control also swung widely in run 2 (dev 248, 1c 544), so these frames are noisy. Run 1 and run 2 1c used builds from before 137bc8a51/acdd7ad74; run 3 is the final code. An in-page probe (EditorView update/measure plus setTimeout/rAF callbacks timed) found the 100–400 ms open frames in both builds are browser rendering, not editor JS: no update over 27 ms. The one idle slice seen took 17 ms at +2.2 s; dev has a 36 ms frame at the same moment. Reading: on Safari the 5k text is shown sooner in every run. Worst frames are flat within this VM's noise, and 1k "live" is mixed (+130, +66, −20).
Author
Owner

iPhone Simulator (std, own iPhone 18 Pro, iOS 27.0, deleted after), 1c build

The 5k-line note opened at the top, then at a far line (?line=4996), in light and dark. The far part is drawn (code block, break). The top pill already names the section (headings parsed so far). Shots attached.

## iPhone Simulator (std, own iPhone 18 Pro, iOS 27.0, deleted after), 1c build The 5k-line note opened at the top, then at a far line (`?line=4996`), in light and dark. The far part is drawn (code block, break). The top pill already names the section (headings parsed so far). Shots attached.
Author
Owner

Perf VM gate: pooled ABAB, 3 sessions, perf-01 (one perf-turn ticket, perf-02 lock held from hd01, 12:08–12:45 CET)

Arms: dev (245875408 web build), dev2 (the same dev build again, the control), new (this branch, product code 137bc8a51). Same server binary for all (dev release, sha256 6aac4843…). Own fixture copy (3,000 notes). Each record is divided by its session's dev mean; mean change with 95% CI.

Typing and list (typing-list-cpu.mjs, 4× throttle, CDP ThreadTime; 12 runs per arm)

metric new vs dev control (dev2 vs dev)
typing CPU / key +0.2% [−0.7, +1.0] −0.2% [−1.1, +0.7]
typing script / key +1.0% [−2.8, +4.8] −1.0% [−4.7, +2.7]
list scroll CPU −0.8% [−2.1, +0.5] +0.1% [−0.9, +1.1]
list script −3.3% [−7.7, +1.0] −3.1% [−6.1, −0.1]

Flat: every interval overlaps the control's.

Open a note in-app (open-note.mjs, 4× throttle; 18 opens per arm per note)

note shown live open CPU idle tail CPU (2 s after)
rich 5k lines −70.3% [−71.3, −69.3] −50.7% [−53.4, −47.9] −48.7% [−51.5, −45.9] +7.3% [+6.7, +7.8]
rich 1k lines −11.4% [−38, +15] −13.9% [−33, +6] −13.4% [−32, +5] +1.1%
prose 4k lines −8.7% [−33, +16] −7.8% [−22, +7] −7.9% [−22, +6] +2.8%

The idle tail is where the deferred parse and paint now run: in idle slices after the note is live, paused while you type or scroll. For the prose note, dev never parsed past viewport + 100k characters; 1c parses to the end at idle, so a later jump or far link costs nothing. That idle work is new: about +2.8% of main-thread time over those 2 s. No interactive path regresses.

## Perf VM gate: pooled ABAB, 3 sessions, perf-01 (one `perf-turn` ticket, perf-02 lock held from hd01, 12:08–12:45 CET) Arms: `dev` (245875408 web build), `dev2` (the same dev build again, the control), `new` (this branch, product code 137bc8a51). Same server binary for all (dev release, sha256 6aac4843…). Own fixture copy (3,000 notes). Each record is divided by its session's dev mean; mean change with 95% CI. **Typing and list (typing-list-cpu.mjs, 4× throttle, CDP ThreadTime; 12 runs per arm)** | metric | new vs dev | control (dev2 vs dev) | |---|---|---| | typing CPU / key | +0.2% [−0.7, +1.0] | −0.2% [−1.1, +0.7] | | typing script / key | +1.0% [−2.8, +4.8] | −1.0% [−4.7, +2.7] | | list scroll CPU | −0.8% [−2.1, +0.5] | +0.1% [−0.9, +1.1] | | list script | −3.3% [−7.7, +1.0] | −3.1% [−6.1, −0.1] | Flat: every interval overlaps the control's. **Open a note in-app (open-note.mjs, 4× throttle; 18 opens per arm per note)** | note | shown | live | open CPU | idle tail CPU (2 s after) | |---|---|---|---|---| | rich 5k lines | **−70.3%** [−71.3, −69.3] | **−50.7%** [−53.4, −47.9] | **−48.7%** [−51.5, −45.9] | +7.3% [+6.7, +7.8] | | rich 1k lines | −11.4% [−38, +15] | −13.9% [−33, +6] | −13.4% [−32, +5] | +1.1% | | prose 4k lines | −8.7% [−33, +16] | −7.8% [−22, +7] | −7.9% [−22, +6] | +2.8% | The idle tail is where the deferred parse and paint now run: in idle slices after the note is live, paused while you type or scroll. For the prose note, dev never parsed past viewport + 100k characters; 1c parses to the end at idle, so a later jump or far link costs nothing. That idle work is new: about +2.8% of main-thread time over those 2 s. No interactive path regresses.
Author
Owner

Merged into dev with --no-ff by the orchestrator (typing/list flat, long-note open much faster; the deferred idle parse after open (+1-7% CPU in the 2 s after open) is noted for kayg). Closing.

Merged into `dev` with `--no-ff` by the orchestrator (typing/list flat, long-note open much faster; the deferred idle parse after open (+1-7% CPU in the 2 s after open) is noted for kayg). Closing.
kayg closed this pull request 2026-11-15 11:53:32 +00:00
Some checks failed
CI / server (pull_request) Has been cancelled
CI / rust (pull_request) Has been cancelled
CI / web (pull_request) Has been cancelled
Markdown extraction contract / markdown-native-contract (pull_request) Has been cancelled
Markdown extraction contract / markdown-editor-contract (pull_request) Has been cancelled

Pull request closed

Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
kayg/calternal!1159
No description provided.