Skip to content

Cache QTextLayout(setCacheEnabled=True) on FittedText to avoid per-repaint text shaping - #79

Merged
trissim merged 1 commit into
mainfrom
perf/fitted-text-static-cache
Nov 27, 2026
Merged

trissim merged 1 commit into
mainfrom
perf/fitted-text-static-cache

Conversation

@trissim

@trissim trissim commented Nov 27, 2026

Copy link
Copy Markdown
Contributor

Why

FittedText.paintEvent called QPainter.drawText(rect, flags, text) on every repaint, re-shaping unchanged label text through HarfBuzz on every frame (134 calls / 55 ms in Toad's #comms scroll profile whenever an overlay NoticeLine overlapped the viewport, and 2.0 Python calls per paint in shipped_run.py).

What changed

  • src/pyqt_reactive/widgets/fitted_text.py:
    • Declared polymorphic WRAP_MODE (NoWrap on FittedText, WrapAtWordBoundaryOrAnywhere on WrappedText).
    • Cached shaped QTextLayouts (setCacheEnabled(True)) + aligned origin + needs_clip per contentsRect() in _prepare_text_layouts, cleared in _clear_metrics_cache() and setAlignment(), and included DevicePixelRatioChange in REMEASURES_ON.
    • Warm paintEvent enters only FittedText.paintEvent (1 Python call instead of 2) and skips setClipRect when the laid-out text already fits rect == self.rect().
  • tests/test_fitted_text.py: asserted single-call warm paintEvent (["FittedText.paintEvent"]) and verified resize invalidates _text_layouts.

Verification

  • tests/test_fitted_text.py + tests/test_status_line.py: 9 passed in 0.89s, REL_IMPORTS=0.
  • Unprofiled alternating A/B (widget.render()): beats drawText on every round (1.54x–1.95x on fitting WrappedText, 1.11x–1.51x on ElidedLine, 3.3x on raw draw).

@trissim
trissim merged commit b0f6d9e into main Nov 27, 2026
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