Every keystroke re-measures the width of every diff line (120 ms per `j` on a 50k-line diff)
Problem
On a large diff each key press lags. On a 50k-line diff (a generated file, a lockfile, a vendored dependency bump) j takes about 120 ms in a real terminal from the key press to the first byte of the new frame; at 10k lines it is about 19 ms, which is already more than one frame. Typing into an annotation behaves the same way, since every character re-renders the diff.
Most of that time goes into viewport.SetContent from bubbles/viewport v1, which runs findLongestLineWidth on every call: a grapheme-by-grapheme ansi.StringWidth over every line of the content. revdiff's own rendering is cheap by now thanks to the per-line cache. In a CPU profile of j on a 50k-line diff, SetContent takes 47% of handleKey; for typing into an annotation it is 61%.
revdiff never uses what that scan computes. The longest line width only feeds bubbles' own horizontal scroll (xOffset), and revdiff scrolls horizontally itself (layout.scrollX, applyHorizontalScroll) and never sets xOffset. The gotchas note on renderDiff already records this (~7.4 ms of a ~17 ms keystroke at 10k lines) as unavoidable "while handing bubbles a full content string".
Measurements
Apple M2 Pro, 160x44, current master (97a5fa7):
| Diff lines | j/k (Update + View) |
annotation keystroke |
|---|---|---|
| 2 000 | 4.6 ms | 3.8 ms |
| 10 000 | 18.9 ms | 14.7 ms |
| 50 000 | 93.5 ms | 68.5 ms |
The j/k column comes from a new BenchmarkModel_CursorKeystroke; none of the existing benchmarks drives cursor movement through Update.
Proposed fix
Replace bubbles/viewport in the diff pane with a small unexported type in app/ui that keeps only what revdiff uses: the Width, Height and YOffset fields, plus SetContent, SetYOffset, GotoTop, TotalLineCount and View. Its SetContent only splits the content. View measures just the visible rows and cuts any row wider than the pane, the case where the bubbles version would have cut it. Call sites stay the same apart from the field type and constructor. About 100 lines plus tests.
Fixing this upstream would not reach revdiff soon: revdiff is on bubbles v1.0.0, and the v2 viewport on bubbles' main branch still runs maxLineWidth over every line in SetContentLines. Moving revdiff to v2 is a much larger change.
I have this working on a branch. Its terminal output is byte-identical to master, and key handling at 10k–50k lines is about twice as fast. Would you take a PR?