Skip to content

perf(text): single-space fast path in wrapLine + unit tests for #128#130

Merged
chiefcll merged 1 commit into
mainfrom
perf/wrapline-space-fast-path
Jul 5, 2026
Merged

perf(text): single-space fast path in wrapLine + unit tests for #128#130
chiefcll merged 1 commit into
mainfrom
perf/wrapline-space-fast-path

Conversation

@chiefcll

@chiefcll chiefcll commented Jul 3, 2026

Copy link
Copy Markdown
Contributor

What

Follow-up to #128 (effective space width in wrapLine):

  • Perf: reuse the precomputed spaceWidth when the separator is exactly one ' ' — the dominant case — instead of calling measureText per word gap. Multi-char runs (multiple spaces, ZWSP runs) are still measured for real. On the Canvas backend the old code paid a ctx.measureText per gap during layout.
  • Tests: adds the unit coverage fix(text): improve effective space width calculation in wrapLine function #128 landed without:
    • multi-space separator counted at its real width ('aa bb' at maxWidth: 55 wraps into two lines — this was the bug fix(text): improve effective space width calculation in wrapLine function #128 fixed and previously overflowed)
    • multi-space separator preserved in output with correct width when the line fits
    • a run of multiple ZWSPs stays a zero-width separator
    • single-space separator uses the precomputed spaceWidth (asserted by passing a spaceWidth that differs from the measured value)

Why

#128 fixed a real overflow bug but merged without tests (repo rule: unit tests required) and regressed the common single-space case from a constant lookup to a measure call per gap.

Notes for reviewers

  • The overflow and breakAll wrap strategies still use the raw single spaceWidth for the separator, so the multi-space underestimate persists on those paths. Pre-existing, out of scope here.
  • Full unit suite passes: 331/331.

🤖 Generated with Claude Code

… for #128

PR #128 made wrapLine measure the real separator string so multi-space
runs are counted at their true width, but that added a measureText call
per word gap even for the dominant single ' ' case — on the Canvas
backend that's a ctx.measureText per gap during layout. Reuse the
precomputed spaceWidth when the separator is exactly one space and only
measure multi-char runs.

Also adds the unit tests #128 landed without: multi-space separators
wrap at their real width and are preserved when the line fits, ZWSP
runs stay zero-width, and the single-space path uses the precomputed
width.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@chiefcll
chiefcll merged commit 534bfa1 into main Jul 5, 2026
3 checks passed
@chiefcll
chiefcll deleted the perf/wrapline-space-fast-path branch July 5, 2026 23:40
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