Repository navigation
perf(desktop): hash each line once in live preview's scan - #216
Merged
Merged
Conversation
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
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
Contributor
Author
|
Cross-review: Ready at The review took 1 pass and 3 re-reviews:
The merge message must not close #193: commit |
bketelsen
approved these changes
Nov 12, 2026
9 tasks done
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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/livefound 2 costs in this repository:1. Live preview's scan (
livepreview.(*Scanner).Scan, about 21% of a keystroke). Of its time:growsliceasResult.Piecesgrew from nothing;The change (
internal/desktop/livepreview/livepreview.go), with the same result:Scannerkeeps 1 entry for each line's text, with its shape and its pieces for each of the 4 combinations ofafterTextandafterGap. So 1 hash for each line, not 2. The key is still a copy of the text, and the cache still clears pastmaxCachetimes the note's lines.ScansizesPieces,Styles,CodeandBlocksfrom the last result, so no append copies them. It does not reuse the last result's slices, which the editor keeps.slices.IsSortedFuncfinds 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.LineStartfor 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-benchin thedesktopjob):mainlive previewc58b98c(both changes)c70080a(the scan's change)1898fdd(the scan's change)ee4f533(both changes; this head, job 131233764635)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
mainpays too. Whether the gate holds on every runner class, or is measured againstmainin 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
1898fddholds 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.jsonmatches.Verification
make checkgreen (host)go test -count=1andgo test -race -count=1on./internal/desktop/livepreview/ ./internal/desktop/editor/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.TestEachLineIsCachedOncechecks 1 entry for each text.TestOverlayKeepsTheTokens(internal/desktop/editor/syntax_overlay_test.go) holdsoverlayto the regex-only path on marked, plain and edge-case lines,longNote(600)and every line ofdocs/specs/markdown.md; it fails with a wrong prefilter ([[[for wikilinks).livepreview.goandlivepreview_test.goto main, keepcache_test.go, then rungo 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).taskset -c 0-1), back to back, 3 runs each: