Skip to content

perf(desktop): hash each line once in live preview's scan - #216

Merged
bketelsen merged 4 commits into
mainfrom
perf/live-preview-typing
Nov 12, 2026
Merged

bketelsen merged 4 commits into
mainfrom
perf/live-preview-typing

Conversation

@frostyard-machines

@frostyard-machines frostyard-machines commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

Summary

A keystroke in live preview scans every line of the note again, and the editor's highlighter tokenizes the lines it shows. A CPU profile of BenchmarkTypingInALongNote/live found 2 costs in this repository:

1. Live preview's scan (livepreview.(*Scanner).Scan, about 21% of a keystroke). Of its time:

  • about 52% was the per-line piece cache: its map lookup, and growslice as Result.Pieces grew from nothing;
  • about 25% was a second map lookup by the same text, for the line's shape;
  • about 6% was a stable sort of pieces that the scan already makes in order.

The change (internal/desktop/livepreview/livepreview.go), with the same result:

  • The Scanner keeps 1 entry for each line's text, with its shape and its pieces for each of the 4 combinations of afterText and afterGap. So 1 hash for each line, not 2. The key is still a copy of the text, and the cache still clears past maxCache times the note's lines.
  • Scan sizes Pieces, Styles, Code and Blocks from the last result, so no append copies them. It does not reuse the last result's slices, which the editor keeps.
  • The pieces are sorted only when slices.IsSortedFunc finds them out of order.

2. The highlighter's overlay (editor.overlay, about 6%): 6 regular expressions on every line it tokenizes. Each now runs only when the literal its match needs is in the line ([!, [[, [^, ==, $, ^); most lines have none. The tokens are the same. This also makes source mode faster.

The rest of a keystroke is MyGo's (rope.LineStart for each replacement, settleLineStyles, wrap measurement, shaping), which this repository does not change.

On the runner. Plan 0009, Phase 5, now records the runner's figures at the gate, each with its CPU, because the runner's CPU changes from run to run (make desktop-bench in the desktop job):

CPU head live preview source mode main live preview
AMD EPYC 9V45 c58b98c (both changes) 10.4 ms 2.4 ms 12.9 ms (run 43147531919)
AMD EPYC 9V74 c70080a (the scan's change) 14.2 ms 3.7 ms 15.1, 15.4 ms (runs 43140903791, 43131798511)
Intel Xeon Platinum 8573C 1898fdd (the scan's change) 17.7 ms 5.1 ms not measured
AMD EPYC 7763 ee4f533 (both changes; this head, job 131233764635) 17.1 ms 4.4 ms not measured; #193 estimates about 18.8 ms

So live preview is under the 16 ms gate on the EPYC 9V45 and 9V74 runners, and over it on the 2 slower classes: the EPYC 7763 (17.1 ms at this head) and the Xeon 8573C (17.7 ms before the overlay's change; at the developer machine's ratio, about 16.8 ms after it). Their source mode takes 1.2 and 1.4 times as long as on the EPYC 9V74. The gate itself is unchanged. The rest of the cost is MyGo's, which main pays too. Whether the gate holds on every runner class, or is measured against main in the same job, or on a pinned runner class, is for the owner to choose, as #193's last comment sets out.

Refs #193. #193's second item (under 16 ms on the runner, or the owner changes the gate) is met on 2 of the 4 runner classes seen, so this PR does not close it. Note: the message of commit 1898fdd holds an issue-closing directive for #193. Squash with an edited commit message that has no such directive, or reopen #193 after a merge commit.

Risk tier

Risk tier: 2 — a performance change in live preview's scan and in the highlighter, with the same output; every existing test passes unchanged. No path in .github/risk-paths.json matches.

Verification

  • make check green (host)
  • go test -count=1 and go test -race -count=1 on ./internal/desktop/livepreview/ ./internal/desktop/editor/
  • Guards: TestASecondScanOfAnEditAllocatesLittle (internal/desktop/livepreview/cache_test.go) bounds a scan of a cached 2,000-line note after a 1-byte edit at 8 allocations; it measures 3 now. TestEachLineIsCachedOnce checks 1 entry for each text. TestOverlayKeepsTheTokens (internal/desktop/editor/syntax_overlay_test.go) holds overlay to the regex-only path on marked, plain and edge-case lines, longNote(600) and every line of docs/specs/markdown.md; it fails with a wrong prefilter ([[[ for wikilinks).
  • Revert test: start from the PR head, revert livepreview.go and livepreview_test.go to main, keep cache_test.go, then run go test -count=1 -run TestASecondScan ./internal/desktop/livepreview/. It fails: allocates 25 times, want at most 8. The overlay's change keeps its output, so its test passes with or without it (it guards equality, not speed).
  • Benchmark, this host, 2 cores (taskset -c 0-1), back to back, 3 runs each:
    • the scan's change: live preview 15.3 → 13.1 ms, 9.61 → 5.90 MB/op; source mode unchanged (3.7 ms).
    • then the overlay's change: live preview 13.5 → 12.9 ms; source mode 3.9 → 3.3 ms.

A live-preview keystroke scans every line of the note. Each line cost 2
map lookups by its text (its shape and its pieces), the result's slices
grew by append from nothing, and the pieces were sorted although the scan
makes them in order.

The Scanner now keeps 1 entry for each line's text, with its shape and its
pieces for each of the 4 combinations of the lines before it. The result's
slices take the last result's sizes, and the pieces are sorted only when
they are out of order. The result is the same.

TestASecondScanOfAnEditAllocatesLittle bounds a scan of a cached note at 8
allocations for 2,000 lines (3 now; 25 before, and thousands without the
cache).

Refs #193
@github-actions github-actions Bot added the level:L3 Skim label Nov 12, 2026
The editor's highlighter lays Obsidian's inline syntax over MyGo's Markdown
tokens with 6 regular expressions on every line it tokenizes. Each now
runs only when the literal its match needs is in the line ([!, [[, [^, ==,
$ or ^): most lines have none. The tokens are the same:
TestOverlayKeepsTheTokens holds overlay to the regex-only path on marked,
plain and edge-case lines, a generated note and the Markdown spec.

Refs #193
@frostyard-machines

Copy link
Copy Markdown
Contributor Author

Cross-review: Ready at ee4f533b85e3e8a798bdad0597fd589ba1bb36c8. Blocking: none. Reviewer: openai/gpt-6.1-sol. Coder: deepseek/deepseek-flash, with test edits by anthropic/claude-opus-5-5. Base b9c6c9006d666183f91313f79648f8323b8a61b0.

The review took 1 pass and 3 re-reviews:

  • At c70080a: Ready, no findings.
  • Re-review 1, at 1898fdd (the plan note). Blocking: no runner figure at the head. Fixed: the figure at ee4f533 is in the PR body.
  • Re-review 2, at ee4f533 (the overlay prefilters and the plan figures by CPU). The prefilters and TestOverlayKeepsTheTokens passed. Blocking: the body held an active issue-closing directive, while the gate is not met on every runner class. Fixed: the body now says Refs desktop: live preview typing is over the 16 ms gate on the CI runner with per-line styles #193. Non-blocking: add the EPYC 7763 figure to the plan. Deferred until the gate is chosen.
  • Re-review 3: both points resolved.

The merge message must not close #193: commit 1898fdd's message has a closing directive. CI is green at ee4f533.

@frostyard-machines frostyard-machines added the ready-for-human Reviewed, CI green: ready for Brian to review and merge label Nov 12, 2026
@bketelsen
bketelsen merged commit c7d68a7 into main Nov 12, 2026
13 checks passed
@bketelsen
bketelsen deleted the perf/live-preview-typing branch November 12, 2026 14:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

level:L3 Skim ready-for-human Reviewed, CI green: ready for Brian to review and merge

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants