perf: cache grapheme widths in Window.print word wrap #286
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.
Problem
Window.print(.word) computed gwidth twice per word: once for the word width check and again per-grapheme while writing cells. This duplicates grapheme width work and adds overhead in word-wrapped text.
Fix
Cache grapheme slices + widths while computing the word width, then reuse that cache when writing cells. If the fixed buffer fills up, reuse the cached prefix and fall back to the original per-grapheme path for the remainder.
Note: Caching uses a fixed 4KB stack buffer to avoid heap allocation for the per-word cache
(ArrayListUnmanaged append).
Bench (local, zig build bench, iterations=200, 80x24)
Baseline = print + extra per-word gwidth pass to mirror the old double-work
Cached = current print implementation
Improvement: Small -17.5% (1.21x); Medium -17.1% (1.21x); Large -16.8% (1.20x); Overflow -15.3% (1.18x).
Tests