Skip to content

Optimize TextLayoutEngine wrap loop#8

Merged
chiefcll merged 1 commit into
mainfrom
optimize-text-layout-engine
May 9, 2026
Merged

Optimize TextLayoutEngine wrap loop#8
chiefcll merged 1 commit into
mainfrom
optimize-text-layout-engine

Conversation

@chiefcll
Copy link
Copy Markdown
Contributor

@chiefcll chiefcll commented May 9, 2026

Summary

Ports lightning-js/renderer#787 — a perf + correctness fix for wrapLine.

  • Replace words.shift() / words.unshift(remainingWord) / spaces.shift() with index pointers (wordIdx, spaceIdx) and a pendingWord string. shift/unshift are O(n); for long lines with many wraps this was O(n²) per word.
  • Replace wrappedLines.push(...wrappedLine) with a for loop. Avoids spread-arg stack risk and is faster on V8 for non-trivial lengths.
  • Fix infinite loop when maxWidth is smaller than a single glyph plus the overflow suffix. Previously, if truncateWord returned word.length === 0 with a remainder, the remainder was unshifted and the loop spun forever. The fix forces an overflowSuffix (or single char fallback) and ends the line as truncated.

Test plan

  • Existing TextLayoutEngine tests pass (21/21)
  • New regression test covers the infinite-loop edge case (maxWidth < glyph + suffix width)

🤖 Generated with Claude Code

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>
@chiefcll chiefcll merged commit a776d50 into main May 9, 2026
1 check failed
@chiefcll chiefcll deleted the optimize-text-layout-engine branch May 9, 2026 22:46
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