perf: cache grapheme widths in Window.print word wrap#286
Open
dyxushuai wants to merge 2 commits intorockorager:mainfrom
Open
perf: cache grapheme widths in Window.print word wrap#286dyxushuai wants to merge 2 commits intorockorager:mainfrom
dyxushuai wants to merge 2 commits intorockorager:mainfrom
Conversation
There was a problem hiding this comment.
Pull request overview
This PR optimizes word-wrapping performance in Window.print by caching grapheme slice positions and widths during the initial width calculation pass, eliminating redundant gwidth() calls when rendering. The optimization uses a fixed 4KB stack buffer to avoid heap allocations and falls back to the original per-grapheme iteration if the buffer overflows.
Key Changes:
- Introduced a caching mechanism that stores grapheme boundaries and widths in a fixed buffer during width calculation
- Refactored the word-rendering path into two branches: a cached path that reuses computed widths, and a fallback path for cache overflow scenarios
- Added comprehensive benchmarks demonstrating ~18-22% performance improvement across different text sizes
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| src/Window.zig | Implements grapheme width caching with WordPiece struct and FixedBufferAllocator, adds cached and fallback rendering paths |
| bench/bench.zig | Adds benchmark infrastructure and test cases (small/medium/large) to measure the caching optimization, includes helper iterators for baseline comparison |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
4 tasks
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.
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