Skip to content

Performance improvements: ASCII width fast paths, viewport-scoped highlights, span scans - #308

Merged
waf merged 1 commit into
mainfrom
performance-improvements-span
Nov 26, 2026
Merged

waf merged 1 commit into
mainfrom
performance-improvements-span

Conversation

@waf

@waf waf commented Nov 26, 2026

Copy link
Copy Markdown
Owner

Per-keystroke render cost for large documents, measured with the existing SteadyState/PerKeystroke benchmarks (main -> this commit):

Changes:

  • UnicodeWidth.GetWidth(char): memoize widths for the ASCII range. Word wrap calls this per character per keystroke, and the Wcwidth lookup behind it costs a HashSet probe plus two dictionary lookups (~70ns/char).

  • UnicodeWidth.GetWidth(span) / GetLengthThatFits: skip runs of printable ASCII with a vectorized IndexOfAnyExceptInRange instead of walking them cluster by cluster. The run's last char is handed back to the UAX-29 walker when the run ends inside the text, since a following combining mark / ZWJ / VS16 clusters onto it.

  • CellRenderer: build the highlight lookup only from spans starting in the visible lines. Previously every span in the document was hashed on every render (~36% of a caret-move keystroke at 1000 lines).

  • Document: line/word boundary scans read the cached text as a span instead of the StringBuilder indexer, which is O(chunks) per access.

  • UnicodeWidthTests: differential tests pinning both fast paths to a walker-only reference over pairwise combinations of hazard strings (combining marks, ZWJ, VS16, CR LF, control chars), across every width budget.

…hlights, span scans

Per-keystroke render cost for large documents, measured with the existing
SteadyState/PerKeystroke benchmarks (main -> this commit):

  Type (insert/delete + render)  @1000 lines: 13,561us -> 1,965us (6.9x)
                                 @100 lines:   1,839us ->   849us (2.2x)
  Navigate (caret move + render) @1000 lines:    844us ->   636us (1.3x)
  WordWrap                       @1000 lines:  7,446us ->   327us (22.8x)

Changes:

- UnicodeWidth.GetWidth(char): memoize widths for the ASCII range. Word wrap
  calls this per character per keystroke, and the Wcwidth lookup behind it
  costs a HashSet probe plus two dictionary lookups (~70ns/char).

- UnicodeWidth.GetWidth(span) / GetLengthThatFits: skip runs of printable
  ASCII with a vectorized IndexOfAnyExceptInRange instead of walking them
  cluster by cluster. The run's last char is handed back to the UAX-29
  walker when the run ends inside the text, since a following combining
  mark / ZWJ / VS16 clusters onto it.

- CellRenderer: build the highlight lookup only from spans starting in the
  visible lines. Previously every span in the document was hashed on every
  render (~36% of a caret-move keystroke at 1000 lines).

- Document: line/word boundary scans read the cached text as a span instead
  of the StringBuilder indexer, which is O(chunks) per access.

- UnicodeWidthTests: differential tests pinning both fast paths to a
  walker-only reference over pairwise combinations of hazard strings
  (combining marks, ZWJ, VS16, CR LF, control chars), across every width
  budget.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@codecov

codecov Bot commented Nov 26, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 88.88889% with 6 lines in your changes missing coverage. Please review.
✅ Project coverage is 86.7%. Comparing base (b7bbd3d) to head (994c1cd).
⚠️ Report is 2 commits behind head on main.

Files with missing lines Patch % Lines
src/PrettyPrompt/Highlighting/CellRenderer.cs 57.1% 4 Missing and 2 partials ⚠️
Additional details and impacted files
@@          Coverage Diff          @@
##            main    #308   +/-   ##
=====================================
  Coverage   86.6%   86.7%           
=====================================
  Files         48      48           
  Lines       4131    4165   +34     
  Branches     685     691    +6     
=====================================
+ Hits        3581    3614   +33     
- Misses       383     386    +3     
+ Partials     167     165    -2     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@waf
waf merged commit c14990e into main Nov 26, 2026
3 checks passed
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.

1 participant