Repository navigation
perf(ui): stop full-diff re-render on annotation cursor blink - #298
Merged
Merged
Conversation
renderDiff rebuilds the styled string for every line of the current file, ~6.7us per line - 72ms at 10k lines, 337ms at 50k. the annotation input is painted inside it, so every message forwarded to the input forced a full re-render, including the textinput cursor blink that fires twice a second and leaves the value untouched. the input now uses a static cursor so no blink timer is scheduled, and the forwarding branch re-renders only when the input value actually changed, which keeps ctrl+v paste repainting. blink cost drops from 86ms to a flat 1.1ms at 10k lines. starting an annotation no longer returns a blink command; the tests asserting a non-nil command are updated to match. adds BenchmarkModel_RenderDiff/_View and BenchmarkModel_AnnotationKeystroke/ _BlinkTick as the measurement harness. typing and j/k navigation still pay the full O(N) render - not addressed here.
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.
a contributor reported revdiff being unusable on large files because it redraws the whole diff on every keystroke. Benchmarked it, and the report is correct - it's just broader than described.
renderDiff()rebuilds the styled string for every line of the current file even though only the viewport's ~40 rows are visible. Measured on an M1 Ultra at 160x44 with the default palette:renderDiff()View()linear, ~6.7us and ~10 allocs per diff line.
View()is flat in N, so the viewport and lipgloss layer is fine - every per-event cost that scales with diff size is aSetContent(m.renderDiff())call.three paths trigger it. This PR fixes one:
model.gogeneric message-forwarding branch - the textinput cursor blink, firing twice a second while merely sitting in annotation mode with nothing typed. Fixed here.annotate.gohandleAnnotateKey- every typed character. Not fixed.diffnav.go, 13 sites - everyj/k/page move. Not fixed.wheel scrolling was already debounced in #179; keys were not.
what changed
the annotation textinput now uses
cursor.CursorStatic, set beforeti.Focus()so no blink command is scheduled and noBlinkMsgtimer exists. A blinking cursor could never be visible here anyway - the input is painted insiderenderDiff, so showing a blink required re-rendering every line.the forwarding branch re-renders only when
m.annot.input.Value()actually changed. That guard is what keeps ctrl+v paste repainting: textinput'sPastecommand produces apasteMsgthat arrives later as a generic message and hits exactly this branch.blink cost drops from 86 ms to a flat 1.1 ms at 10k lines, and in production the timer is gone entirely so the real cost is zero.
consequence for tests
starting an annotation now returns a nil
tea.Cmd, since that command was only ever the blink. 16 assertion sites across 4 test files asserted it was non-nil and are updated to assert nil. None of them executed the command, and each test's real subject (typicallyannotating == true) is untouched.what's left
typing still costs a full O(N) render per character - 82 ms at 10k lines is roughly a 12 char/sec ceiling - and navigation pays the same. Fixing that means rendering only the visible window instead of all N lines, which touches scrollbar row math, cursor pinning and search. Deliberately not attempted here.
BenchmarkModel_RenderDiff,_View,_AnnotationKeystrokeand_AnnotationBlinkTickare added as the harness so that change can be measured rather than guessed at. NotebenchModelneeds both thefilesLoadedMsgand the seq-matchedfileLoadedMsgorViewshort-circuits to"loading files..."and the numbers become meaningless - that's recorded in the gotchas file.TestModel_AnnotatingNoOpMessageDoesNotRerenderDiffpins the fix and was verified to fail without it.