Skip to content

Resource dictionary lookups allocate an iterator on every walk #24522

Closed as duplicate

Description

@MartinZikmund

What would you like to be added? 💡

DependencyObject.GetResourceDictionaries returns the ResourceDictionaries in scope for an object, nearest first. It is a C# iterator (yield return), so every call allocates a compiler-generated state machine before producing a single dictionary — including the very common case where no ancestor carries resources and the walk yields nothing at all.

It runs on the hottest resource paths:

  • once per theme reference, from Phase A of UpdateThemeReference — an object with N {ThemeResource} references performs N separate ancestor walks and allocates N iterators (the code's own comment cites ListViewItemPresenter carrying 41+ theme refs)
  • twice per element Enter for the implicit-style lookup, since ApplyStyles runs from both EnterImpl and the Loading pass
  • on every visual-tree resource retrieval, from ResourceResolver.TryVisualTreeRetrieval

An allocation profile of a scrolling ListView puts the generated iterator type at the top of the table, ahead of every text-shaping type; it ranks first in template build as well.

Why is this needed? 🤔

A struct enumerator that visits the same dictionaries in the same order removes the allocation without touching resolution semantics. The ancestor order is load-bearing (nearest scope wins, then the app resources), so it is reproduced exactly rather than re-derived; callers that genuinely want an array keep using the iterator.

Measured impact 📊

Managed bytes allocated per iteration on the UI thread (GC.GetAllocatedBytesForCurrentThread, median of 7 rounds, two runs per side; figures reproduce within ~1%):

Workload Before After Δ
Enter + Leave TextBox 30,590 B 22,152 B −27.6 %
Enter + Leave Button 18,592 B 14,520 B −21.9 %
Enter + Leave a 40-control page 796,857 B 650,481 B −18.4 %
Enter + Leave CheckBox 24,744 B 20,488 B −17.2 %
Enter + Leave ListViewItem 37,828 B 32,024 B −15.3 %
Enter + Leave ToggleSwitch 48,139 B 41,292 B −14.2 %
ListView (2,000 items) scroll step 178,516 B 157,212 B −11.9 %
Theme toggle on that page 540,152 B 509,512 B −5.7 %

Over a longer run of the same scroll workload — 1,000 steps, so the collection count is a resolvable integer rather than the 0–2 a short run produces:

Metric (1,000 scroll steps) Before After Δ
Allocated per step 189,498 B 168,583 B −11.0 %
Gen0 collections, total 15 13 −2

Both sides reproduced their gen0 count exactly across two runs.

Renderer 🎨

  • Skia

For which platforms 📱💻🖥️

All platforms 🌍

Anything else we need to know? 💬

Measured with a headless Skia app (Uno.UI.Runtime.Skia.Headless) that project-references the framework, with Fluent resources merged. Wall-clock is not reported: it varied by more than the effect between identical runs on the measuring machine. Gen0 collection counts were captured but at 0–2 per round are too coarse to resolve a difference.

A separate, larger opportunity remains on the same path and is deliberately not covered here: UpdateAllThemeReferences performs one full ancestor walk per theme reference, so an object with N references walks the tree N times. Collapsing that into a single walk needs care, because the surrounding code notes that updating one reference can cascade and modify the theme-reference map, and a newly resolved Style can change the ancestor scope mid-loop.

Activity

  1. added
    triage/untriaged
    on Sep 16, 2026
  2. MartinZikmund commented on Oct 14, 2026

    @MartinZikmund
    MemberAuthor

    Closing as a duplicate of #24225. Any details that were only here have been copied over to #24225.

  3. Jen-Uno commented on Oct 28, 2026

    @Jen-Uno
    Contributor

    This issue was closed as a duplicate, so there is nothing for QA to verify. Moving it from Ready for QA Review to Done on the Uno Team Backlog.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    triage/untriaged

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions