Repository navigation
Gtk3Backend: reuse Pango context and layout in text measurement - #820
Merged
Merged
Conversation
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
approved these changes
Nov 22, 2026
stackotter
left a comment
Collaborator
There was a problem hiding this comment.
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?
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.
Problem
Pango.init(for:)callsgtk_widget_create_pango_context(), which allocates a fresh PangoContext (including font map resolution) on every call, andgetTextSize()allocates a newPangoLayoutper 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:
NavigationSplitViewpane handle back and forth for a few secondsi.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
gtk_widget_get_pango_context()— the context GTK already caches on the widget (borrowed reference, so nog_object_unref).PangoLayoutperPangoinstance and re-set text/ellipsize/width/height per measurement.Measurement (same app, same interaction, only this patch applied)
Rendering output is unchanged (pixel-level comparison of screenshots before/after shows the same layout), and no GLib warnings/criticals are emitted.