Skip to content

DependencyObject.GetResourceDictionaries allocates an iterator on every call #24225

Open

Description

@MartinZikmund

What would you like to be added? 💡

GetResourceDictionaries is a yield return iterator, so every call allocates a state machine. It is called once per theme reference per element entering the tree, on top of every implicit-style lookup (GetImplicitStyle) and every {StaticResource} visual-tree retrieval (ResourceResolver.TryVisualTreeRetrieval).

While a virtualised ListView materialises containers during a scroll, <GetResourceDictionaries>d__219 is the single largest allocator in the profile at 6.3 %, reached through FrameworkElement.OnFwEltLoading → UpdateResourceBindings → UpdateAllThemeReferences → UpdateThemeReference.

Why is this needed? 🤔

Returning a readonly struct enumerable with a struct enumerator removes the allocation while leaving the walk unchanged — still lazy, still yielding the containing dictionary first, then non-empty ancestor Resources nearest-first, then Application.Resources, and still short-circuiting at the caller's first match. It still implements IEnumerable<ResourceDictionary> so the two cold .ToArray() callers keep working, while foreach binds to the struct enumerator and allocates nothing.

WinUI's equivalent walk (ScopedResources::TraverseVisualTreeResources) is likewise a plain loop that allocates nothing, so this moves Uno towards WinUI rather than away.

Measured impact 📊

Workload Metric Before After Δ
list.scroll-step alloc / step 154,522 B 133,264 B −13.8 %
list.scroll-step alloc / realised container 6,718.4 B 5,794.1 B −13.8 %
list.scroll-step time / step 1.6573 ms 1.6444 ms −0.8 % (noise)
control layout.full-pass alloc 4,096,383 B 4,095,809 B ±0
control grid.full-pass alloc 51,833 B 51,833 B ±0
control tree.build alloc 21,929,730 B 21,976,875 B ±0
control dp.set-get alloc 48,001 B 48,001 B ±0

A ListView over 2,000 items with a two-TextBlock DataTemplate and 23 realised containers, scrolled 137 px per step (deliberately not a multiple of the item height, so every step straddles items and forces recycling), driven through the dispatcher tick. 3 runs of the full workload set in identical order, median.

The layout and grid workloads are unmoved because their trees are dirtied in place rather than re-entered — this cost is paid on Enter, which is what list virtualisation does constantly.

Renderer 🎨

  • Skia

For which platforms 📱💻🖥️

All platforms 🌍

Anything else we need to know? 💬

Found by allocation profiling — dotnet-trace attributed by type and call stack — of a headless app that project-references the real framework, then measured before/after with the same harness. Allocation figures are deterministic (byte-identical across runs); wall-clock is reported only from a quiet machine alongside untouched control workloads that would expose drift. Full methodology, raw runs and control workloads: specs/059-performance-improvements/evidence.md.

Activity

  1. added
    triage/untriaged
    on Aug 27, 2026
  2. MartinZikmund commented on Oct 14, 2026

    @MartinZikmund
    MemberAuthor

    Details from duplicate #24522 that are not in this issue yet:

    • Extra call sites: once per {ThemeResource} reference from Phase A of UpdateThemeReference (N references mean N walks and N iterators; ListViewItemPresenter has 41+); twice per element Enter for the implicit-style lookup (ApplyStyles runs from both EnterImpl and the Loading pass); and every ResourceTreeRetrieval via ResourceResolver.TryVisualTreeRetrieval.
    • Allocations per iteration on the UI thread (GC.GetAllocatedBytesForCurrentThread, median of 7 rounds, headless Skia):
      • Enter+Leave TextBox 30,590 -> 22,152 B (-27.6%)
      • Button -21.9%
      • a 40-control page 796,857 -> 650,481 B (-18.4%)
      • CheckBox -17.2%
      • ListViewItem -15.3%
      • ToggleSwitch -14.2%
      • ListView (2,000 items) scroll step -11.9%
      • theme toggle -5.7%
    • 1,000-step scroll: 189,498 -> 168,583 B/step (-11.0%), Gen0 collections 15 -> 13.
    • The iterator type ranks first in template build as well.
  3. added
    7.1
    on Oct 14, 2026
  4. removed
    7.1
    on Oct 29, 2026
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