Skip to content

perf(ui): stop full-diff re-render on annotation cursor blink - #298

Merged
umputun merged 2 commits into
masterfrom
perf/annotation-blink-rerender
Jul 30, 2026
Merged

umputun merged 2 commits into
masterfrom
perf/annotation-blink-rerender

Conversation

@umputun

@umputun umputun commented Jul 30, 2026

Copy link
Copy Markdown
Owner

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:

file lines renderDiff() View() keystroke (Update+View)
500 3.5 ms 1.1 ms 5.1 ms
2,000 14 ms 1.2 ms 17 ms
10,000 72 ms 1.2 ms 82 ms
50,000 337 ms 1.3 ms 386 ms

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 a SetContent(m.renderDiff()) call.

three paths trigger it. This PR fixes one:

  • model.go generic message-forwarding branch - the textinput cursor blink, firing twice a second while merely sitting in annotation mode with nothing typed. Fixed here.
  • annotate.go handleAnnotateKey - every typed character. Not fixed.
  • diffnav.go, 13 sites - every j/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 before ti.Focus() so no blink command is scheduled and no BlinkMsg timer exists. A blinking cursor could never be visible here anyway - the input is painted inside renderDiff, 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's Paste command produces a pasteMsg that 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 (typically annotating == 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, _AnnotationKeystroke and _AnnotationBlinkTick are added as the harness so that change can be measured rather than guessed at. Note benchModel needs both the filesLoadedMsg and the seq-matched fileLoadedMsg or View short-circuits to "loading files..." and the numbers become meaningless - that's recorded in the gotchas file.

TestModel_AnnotatingNoOpMessageDoesNotRerenderDiff pins the fix and was verified to fail without it.

umputun added 2 commits July 30, 2026 12:12
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.
Copilot AI review requested due to automatic review settings July 30, 2026 18:10

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@umputun
umputun merged commit faa338b into master Jul 30, 2026
5 checks passed
@umputun
umputun deleted the perf/annotation-blink-rerender branch July 30, 2026 18:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants