Optimize TextLayoutEngine wrap loop#8
Merged
Conversation
Port lightning-js/renderer#787: - Replace shift/unshift on words/spaces arrays with index pointers and a pendingWord string to avoid O(n^2) array reshuffling on long lines. - Replace spread push of wrappedLine with a for loop. - Guard against infinite loop when maxWidth is smaller than a single glyph plus the overflow suffix; force overflowSuffix (or single char) and end the line as truncated. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
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.
Summary
Ports lightning-js/renderer#787 — a perf + correctness fix for
wrapLine.words.shift()/words.unshift(remainingWord)/spaces.shift()with index pointers (wordIdx,spaceIdx) and apendingWordstring.shift/unshiftare O(n); for long lines with many wraps this was O(n²) per word.wrappedLines.push(...wrappedLine)with aforloop. Avoids spread-arg stack risk and is faster on V8 for non-trivial lengths.maxWidthis smaller than a single glyph plus the overflow suffix. Previously, iftruncateWordreturnedword.length === 0with a remainder, the remainder was unshifted and the loop spun forever. The fix forces anoverflowSuffix(or single char fallback) and ends the line as truncated.Test plan
TextLayoutEnginetests pass (21/21)maxWidth < glyph + suffix width)🤖 Generated with Claude Code