Skip to content

perf(theming): Cut per-reference theme resource allocations - #25403

Open
MartinZikmund wants to merge 2 commits into
masterfrom
dev/mazi/themeresource-perf
Open

MartinZikmund wants to merge 2 commits into
masterfrom
dev/mazi/themeresource-perf

Conversation

@MartinZikmund

@MartinZikmund MartinZikmund commented Oct 13, 2026 •

Copy link
Copy Markdown
Member

GitHub Issue: closes #25396

PR Type:

✨ Feature (performance)

What changed? 🚀

Each {ThemeResource} reference now allocates much less per element. Motivating case: the WinUI ListViewItemPresenter template that #25389 adopts for ListView/GridView. It carries 41 theme references per container, and it made GridView slower than the old template.

  • ResourceBinding is a plain object, no longer a BindingBase/DependencyObject. ResourceBindingCollection is a flat list scanned linearly, like ThemeResourceMap, instead of a dictionary per property.
  • Theme references share one WeakReference per ResourceDictionary (ResourceDictionary.WeakSelf) instead of allocating one each.
  • The ancestor lookup in UpdateThemeReference no longer allocates an iterator. It still walks once per reference, as WinUI does.

The duplicate Loading-time theme pass, also listed in #25396, is now its own PR.

Numbers. Win32 Skia, Release, local ListView/GridView harness with a non-virtualized 1,000-tile GridView.

Stacked on #25389, single round, before → after this change alone:

Metric Before After
GridView realize alloc/container 207,275 B 155,301 B (−25%)
ListView realize alloc/container 316,623 B 258,865 B (−18%)

Allocation trace for the whole GridView-1000 run: ResourceBinding −172 MB, ancestor iterator −186 MB, the per-property binding dictionaries (~166 MB) gone, per-reference WeakReference −42 MB.

On master alone, 3 rounds plus a null copy, measured together with the Loading-pass PR:

Metric master + both PRs
GridView realize alloc/container 123.9 KB 107.7 KB (−13%)
ListView realize alloc/container 402.7 KB 381.0 KB (−5.4%)
Steady-state heap/container, ListView / GridView 339 / 137 KB 328 / 130 KB

Timings on master alone were within noise.

Validation.

  • Runtime (Skia desktop): Given_ThemeResource, Given_ElementTheme, Given_Theme_Materialization, Given_ResourceDictionary, Given_FrameworkElement_ThemeResources, Given_Control all pass (185 tests).
  • Unit: ~Theme|~Resource all pass (453), including the new Given_ResourceBindingCollection.

PR Checklist ✅

🤖 Generated with Claude Code

https://claude.ai/code/session_01Vi68uWCYVXxhcX9m6JEwSC

MartinZikmund and others added 2 commits October 13, 2026 21:01
Every {ThemeResource} on an element allocated a ResourceBinding that was
a full DependencyObject, a per-property dictionary to hold it, its own
WeakReference to the pinned dictionary and an ancestor-dictionary
iterator on each refresh. Templates with many theme references pay this
per container; the ListViewItemPresenter template carries 41.

- ResourceBinding is a plain object; the collection is a flat list
- theme references share one weak reference per ResourceDictionary
- the ancestor lookup in UpdateThemeReference no longer allocates

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Vi68uWCYVXxhcX9m6JEwSC
@MartinZikmund
MartinZikmund force-pushed the dev/mazi/themeresource-perf branch from 97785e1 to c7548b5 Compare October 13, 2026 19:06
@MartinZikmund MartinZikmund changed the title perf(theming): Cut the per-reference cost of {ThemeResource} perf(theming): Cut per-reference theme resource allocations Oct 13, 2026
@github-actions

github-actions Bot commented Oct 13, 2026 •

Copy link
Copy Markdown
Contributor

Claude finished @MartinZikmund's task in 34s —— View job


Review summary

Verdict: fix-first. The code in the diff looks sound. The main problem is that the PR description doesn't match what the branch contains.

Issues: 1 major (below), 2 minor (inline).

Cross-cutting concerns

  • The Loading-time skip is missing from the branch. The description says FrameworkElement.OnLoadingPartial now skips UpdateAllThemeReferences when Enter already resolved everything. That change isn't in the diff:
    • Neither commit touches FrameworkElement.cs.
    • OnLoadingPartial still calls UpdateThemeBindings(ResourceUpdateReason.ResolvedOnLoading) unconditionally (FrameworkElement.cs:387).
    • Either the change wasn't pushed, or the description is stale.
    • Please push it, or drop that section and the claims that depend on it, including the measurements and the semantics note about ancestor Resources changes.
    • If it lands, it needs runtime tests for the cases that must still re-run: style and visual-state setters, a failed refresh, an element that left the tree, and the hot reload reason.
  • Test coverage.
    • Given_ResourceBindingCollection covers the flat list well.
    • Nothing exercises the new TryGetValueFromResourceDictionaries against the old GetResourceDictionaries(includeAppResources: false) ordering and parent walk. That includes the non-FrameworkElement branch, which uses VisualTreeHelper.GetParent. The existing runtime tests may cover it indirectly, but a targeted test would pin the equivalence.
  • Other checks.
    • ResourceBinding is now a sealed plain class. I found no remaining BindingBase consumers of it in non-test code.
    • Style, braces and tabs conform to AGENTS.md.
    • The commit messages are Conventional Commits.
  • Not run. I reviewed by inspection only and did not build or run tests. The PR's runtime and unit test results are the author's.

// and new items can be added when resource bindings are evaluated.
=> _cachedAllBindings ??= _bindings.ToArray();

public IEnumerable<ResourceBinding>? GetBindingsForProperty(DependencyProperty property)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

GetBindingsForProperty now allocates a List<ResourceBinding> on every hit. The old version returned Dictionary.Values and didn't allocate a list. This PR is about cutting allocations, so check the callers. If any run per reference, consider a non-allocating shape: a TryGet(property, precedence) or a struct enumerator over the flat list. Or add a note on why the allocation is acceptable.


var binding = new ResourceBinding(resourceKey, updateReason, context, precedence ?? DependencyPropertyValuePrecedences.Local, setterBindingPath);
SetBindingInternal(dependencyProperty, binding);
if (dependencyProperty == null)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Minor: this no longer goes through SetBindingInternal, so the null check and TryRegisterInheritedProperties(force: true) are duplicated here. The original order ran TryRegisterInheritedProperties before the null check; here the check runs first, which is harmless. Consider a short comment saying the registration call is kept deliberately for parity, so it isn't dropped later as dead code.

@github-actions

github-actions Bot commented Oct 13, 2026 •

Copy link
Copy Markdown
Contributor

Claude finished @MartinZikmund's task in 25s —— View job


Review summary

Verdict: fix-first (one description/code mismatch to resolve), otherwise looks good.

Issues: 1 medium, 0 high, 0 low.

  • The ResourceBinding change, the flat ResourceBindingCollection, the shared WeakSelf and the allocation-free ancestor lookup look correct and match the surrounding style. The new unit tests cover the collection.
  • The PR description says the Loading-time theme pass is skipped. That change isn't in the diff (see the inline comment). I only read the code. I didn't build or run any tests.

}

TryRegisterInheritedProperties(force: true);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The PR description says FrameworkElement.OnLoadingPartial now skips the Loading-time theme pass when Enter already resolved everything, but the diff doesn't touch FrameworkElement.cs. OnLoadingPartial (FrameworkElement.cs:373-387) still calls UpdateThemeBindings(ResourceUpdateReason.ResolvedOnLoading) unconditionally. Either that change is missing from the branch or the description is stale. The measurements and the stated behavior change (ancestor Resources edits between Enter and Loading no longer picked up) should match what ships.

@github-actions github-actions Bot added the area/automation Categorizes an issue or PR as relevant to project automation label Oct 13, 2026
@unodevops

Copy link
Copy Markdown
Contributor

@nventive-devops

Copy link
Copy Markdown
Contributor

The build 239296 found UI Test snapshots differences: skia-linux-screenshots: 118, skia-windows-screenshots: 2458

Details
  • skia-linux-screenshots: 118 changed over 2458

    🚨🚨 Comparison Details (first 20) 🚨🚨
    • Buttons.png-dark
    • Buttons.png
    • CalendarView_Theming.png-dark
    • CalendarView_Theming.png
    • ClipboardTests.png-dark
    • DisplayInformation.png-dark
    • ButtonClippingTestsControl.png-dark
    • ButtonClippingTestsControl.png
    • CompositionEffectBrush.png-dark
    • CompositionEffectBrush.png
    • CompositionNineGridBrush_Source_Changes.png-dark
    • CompositionNineGridBrush_Source_Changes.png
    • DropDownButtonPage.png-dark
    • Examples.png
    • DoubleImageBrushInList.png-dark
    • DoubleImageBrushInList.png
    • Gamepad_CurrentReading.png-dark
    • Gamepad_Enumeration.png-dark
    • Image_UseTargetSizeLate.png-dark
    • Image_UseTargetSizeLate.png
  • skia-windows-screenshots: 2458 changed over 2488

    🚨🚨 Comparison Details (first 20) 🚨🚨
    • Accessibility_ScreenReader.png
    • AppBar_KeyBoard.png-dark
    • ApplicationViewSpanningRectsPage.png-dark
    • AppWindowFeatures.png
    • AppXamlDefinedResources.png
    • Attributed_text_Supserscript.png-dark
    • AutoBorderStretchwithleftmargin.png-dark
    • AutomationPropertiesExtensions_Role.png
    • AutoSizedTopLeft.png
    • AutoSuggestBoxChosenSuggestion.png-dark
    • BackGesture.png-dark
    • BackGesture.png
    • Basics.png-dark
    • Basics.png
    • BindableDrawerLayout_ChangePane.png-dark
    • BindableDrawerLayout_ChangePane.png
    • BorderCenteredwithmargins.png-dark
    • BorderCenteredwithmargins.png
    • BorderLeftwithmargins.png-dark
    • BorderLeftwithmargins.png

@unodevops

Copy link
Copy Markdown
Contributor

⚠️⚠️ The build 239296 has failed on Uno.UI - CI.

@MartinZikmund MartinZikmund added the 7.1 PRs only. For issues, use the Release field. label Oct 14, 2026
MartinZikmund added a commit that referenced this pull request Nov 2, 2026
GridView stays on GridViewItemExpanded until its perf PRs (#25403, #25404, #25406, #25409, #25411, #25413) land. Apps opt into WinUI's chrome with DefaultGridViewItemStyle.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01TXMNu2NWgGKJFTAYtNHkvT
MartinZikmund added a commit that referenced this pull request Nov 2, 2026
The implicit GridViewItem style is BasedOn DefaultGridViewItemStyle again,
as in WinUI, so default GridView items render through the ListViewItemPresenter
chrome and default to AllowDrop=True. The Fluent GridViewItemExpanded is removed.

Merge after #25403, #25404, #25406, #25409, #25411 and #25413.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01TXMNu2NWgGKJFTAYtNHkvT
MartinZikmund added a commit that referenced this pull request Nov 9, 2026
GridView stays on GridViewItemExpanded until its perf PRs (#25403, #25404, #25406, #25409, #25411, #25413) land. Apps opt into WinUI's chrome with DefaultGridViewItemStyle.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01TXMNu2NWgGKJFTAYtNHkvT

@morning4coffe-dev morning4coffe-dev left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

TryGetValueFromResourceDictionaries walks the same dictionaries in the same order as GetResourceDictionaries(includeAppResources: false), with the same shouldCheckSystem: false lookup, so behavior is unchanged. Sharing one WeakReference per ResourceDictionary mirrors WinUI, where CThemeResource holds an xref::weakref_ptr<CResourceDictionary> (components/theming/inc/ThemeResource.h) whose control block is shared per object, and nothing calls SetTarget on it. The flat ResourceBindingCollection keeps the replace-per-(property, precedence) semantics, and the unit tests cover it. LGTM.

@mergify

mergify Bot commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

Tick the box to add this pull request to the merge queue (same as @mergifyio queue). If it does not match the queue conditions yet, it enters the queue once it does.

  • Queue this pull request

MartinZikmund added a commit that referenced this pull request Nov 13, 2026
GridView stays on GridViewItemExpanded until its perf PRs (#25403, #25404, #25406, #25409, #25411, #25413) land. Apps opt into WinUI's chrome with DefaultGridViewItemStyle.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01TXMNu2NWgGKJFTAYtNHkvT
MartinZikmund added a commit that referenced this pull request Nov 13, 2026
GridView stays on GridViewItemExpanded until its perf PRs (#25403, #25404, #25406, #25409, #25411, #25413) land. Apps opt into WinUI's chrome with DefaultGridViewItemStyle.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01TXMNu2NWgGKJFTAYtNHkvT
MartinZikmund added a commit that referenced this pull request Nov 13, 2026
GridView stays on GridViewItemExpanded until its perf PRs (#25403, #25404, #25406, #25409, #25411, #25413) land. Apps opt into WinUI's chrome with DefaultGridViewItemStyle.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01TXMNu2NWgGKJFTAYtNHkvT
MartinZikmund added a commit that referenced this pull request Nov 13, 2026
GridView stays on GridViewItemExpanded until its perf PRs (#25403, #25404, #25406, #25409, #25411, #25413) land. Apps opt into WinUI's chrome with DefaultGridViewItemStyle.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01TXMNu2NWgGKJFTAYtNHkvT
MartinZikmund added a commit that referenced this pull request Nov 14, 2026
GridView stays on GridViewItemExpanded until its perf PRs (#25403, #25404, #25406, #25409, #25411, #25413) land. Apps opt into WinUI's chrome with DefaultGridViewItemStyle.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01TXMNu2NWgGKJFTAYtNHkvT
MartinZikmund added a commit that referenced this pull request Nov 17, 2026
GridView stays on GridViewItemExpanded until its perf PRs (#25403, #25404, #25406, #25409, #25411, #25413) land. Apps opt into WinUI's chrome with DefaultGridViewItemStyle.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01TXMNu2NWgGKJFTAYtNHkvT
MartinZikmund added a commit that referenced this pull request Nov 17, 2026
GridView stays on GridViewItemExpanded until its perf PRs (#25403, #25404, #25406, #25409, #25411, #25413) land. Apps opt into WinUI's chrome with DefaultGridViewItemStyle.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01TXMNu2NWgGKJFTAYtNHkvT
MartinZikmund added a commit that referenced this pull request Nov 17, 2026
GridView stays on GridViewItemExpanded until its perf PRs (#25403, #25404, #25406, #25409, #25411, #25413) land. Apps opt into WinUI's chrome with DefaultGridViewItemStyle.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01TXMNu2NWgGKJFTAYtNHkvT
MartinZikmund added a commit that referenced this pull request Nov 17, 2026
GridView stays on GridViewItemExpanded until its perf PRs (#25403, #25404, #25406, #25409, #25411, #25413) land. Apps opt into WinUI's chrome with DefaultGridViewItemStyle.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01TXMNu2NWgGKJFTAYtNHkvT
MartinZikmund added a commit that referenced this pull request Nov 18, 2026
GridView stays on GridViewItemExpanded until its perf PRs (#25403, #25404, #25406, #25409, #25411, #25413) land. Apps opt into WinUI's chrome with DefaultGridViewItemStyle.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01TXMNu2NWgGKJFTAYtNHkvT
MartinZikmund added a commit that referenced this pull request Nov 18, 2026
GridView stays on GridViewItemExpanded until its perf PRs (#25403, #25404, #25406, #25409, #25411, #25413) land. Apps opt into WinUI's chrome with DefaultGridViewItemStyle.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01TXMNu2NWgGKJFTAYtNHkvT
MartinZikmund added a commit that referenced this pull request Nov 18, 2026
GridView stays on GridViewItemExpanded until its perf PRs (#25403, #25404, #25406, #25409, #25411, #25413) land. Apps opt into WinUI's chrome with DefaultGridViewItemStyle.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01TXMNu2NWgGKJFTAYtNHkvT
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

7.1 PRs only. For issues, use the Release field. area/automation Categorizes an issue or PR as relevant to project automation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Reduce the per-reference cost of applying {ThemeResource}

4 participants