Skip to content

Gtk3Backend: reuse Pango context and layout in text measurement - #820

Merged
stackotter merged 1 commit into
moreSwift:mainfrom
yangl326-Dylan:fix/gtk3-pango-reuse
Nov 22, 2026
Merged

stackotter merged 1 commit into
moreSwift:mainfrom
yangl326-Dylan:fix/gtk3-pango-reuse

Conversation

@yangl326-Dylan

Copy link
Copy Markdown
Contributor

Problem

Pango.init(for:) calls gtk_widget_create_pango_context(), which allocates a fresh PangoContext (including font map resolution) on every call, and getTextSize() allocates a new PangoLayout per measurement.

SwiftCrossUI measures every text node through this path on each layout pass, so interactive operations that trigger repeated layout pay this cost over and over. In a real GTK3 app (a three-pane tool window with a sidebar/list/detail split) we measured the main thread at:

  • 32% CPU while dragging a NavigationSplitView pane handle back and forth for a few seconds
  • 73% CPU across three toolbar button clicks (each click triggers a state change + relayout)
  • ~1% CPU when idle

i.e. the UI thread spends nearly all of its time in Pango setup rather than layout or drawing, which shows up as laggy dragging and unresponsive-feeling clicks.

Fix

  • Use gtk_widget_get_pango_context() — the context GTK already caches on the widget (borrowed reference, so no g_object_unref).
  • Allocate one PangoLayout per Pango instance and re-set text/ellipsize/width/height per measurement.
  • Reset width/height to -1 when no proposal is given (previously the stale proposal leaked into subsequent measurements of the same context; each call now gets the exact proposal it's given).

Measurement (same app, same interaction, only this patch applied)

Scenario Before After
Paned drag (~5 s) 32% CPU 3% CPU
3 button clicks (~6 s) 73% CPU 1% CPU

Rendering output is unchanged (pixel-level comparison of screenshots before/after shows the same layout), and no GLib warnings/criticals are emitted.

gtk_widget_create_pango_context() allocates a fresh PangoContext (including
font map resolution) for every call, and getTextSize() allocated a new
PangoLayout per measurement. SCUI measures every text node on each layout
pass, so state updates and interactive resizing (paned drags in particular)
paid this cost repeatedly: in a real GTK3 app we measured the main thread
burning 32% CPU during a paned drag and 73% CPU across three button clicks,
while staying near-idle otherwise.

Switch to gtk_widget_get_pango_context() (the GTK-cached, borrowed context)
and reuse a single PangoLayout per Pango instance, resetting text/ellipsize/
width/height per measurement.

Same app after the change: paned drag 32% -> 3% CPU, three button clicks
73% -> 1% CPU. Rendering output is identical.

@stackotter stackotter left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for looking into this performance issue, the fix seems sensible, and the results look promising! I'll merge once CI passes (just approved it now).

Please also take note of our LLM policy. We require AI usage to be disclosed.

And if you don't mind me asking; what sort of app are you creating with SwiftCrossUI?

@stackotter
stackotter merged commit 8b39220 into moreSwift:main Nov 22, 2026
20 checks passed
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.

2 participants