Skip to content

Enter, Leave and theme walks box an enumerator per DependencyObject collectionΒ #24517

Closed

Description

@MartinZikmund

What would you like to be added? πŸ’‘

The element Enter, Leave and resource-binding walks visit the collections a property holds through IEnumerable:

  • DependencyObject.EnterProperties / LeaveProperties β€” foreach over a value typed IEnumerable
  • DependencyObject.InnerUpdateChildResourceBindings β€” foreach over a value typed IEnumerable<DependencyObject>
  • the IAdditionalChildrenProvider branch of all three (Storyboard.Children)
  • TextBlock.UpdateThemeBindings β€” foreach over InlineCollection

DependencyObjectCollection<T>.GetEnumerator() returns IEnumerator<T>, so each of those loops boxes a List<T>.Enumerator (40 B on x64). VisualStateManager.VisualStateGroups is stored by XAML codegen as a VisualStateGroup[], which boxes an array enumerator on the same walks.

A templated control's VisualStateGroups, States, Setters, StateTriggers and Transitions each pay this on every Enter, every Leave and every theme update β€” so in a virtualized list, on every recycled container. An allocation profile of a scrolling ListView shows Enumerator[StateTriggerBase], Enumerator[VisualState], Enumerator[SetterBase], Enumerator[VisualTransition] and Enumerator[Inline] among the top allocated types, all rooted at these walks.

Why is this needed? πŸ€”

Indexing the backing list instead removes the allocation while visiting the same items in the same order, which is also how WinUI's CDOCollection::EnterImpl / LeaveImpl walk m_items. Values that are neither a framework DO collection nor an array keep their existing enumeration.

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 Button 18,592 B 17,376 B βˆ’6.5 %
Enter + Leave CheckBox 24,744 B 21,848 B βˆ’11.7 %
Enter + Leave ToggleSwitch 48,215 B 44,576 B βˆ’7.5 %
Enter + Leave ListViewItem 37,768 B 32,872 B βˆ’13.0 %
Enter + Leave TextBox 30,416 B 27,024 B βˆ’11.2 %
Enter + Leave a 40-control page 796,817 B 721,057 B βˆ’9.5 %
Theme toggle on that page 539,832 B 514,872 B βˆ’4.6 %
ListView (2,000 items) scroll step 177,950 B 161,173 B βˆ’9.4 %

The saving is an exact multiple of 40 B in every case, i.e. exactly the enumerator boxes.

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 deliberately not reported: it varied by 2Γ— between identical runs on the measuring machine, far above the effect. Gen0 collection counts were also captured, but at 0–2 per round they are too coarse to show a difference.

This overlaps with the empty-state-trigger work: on the scroll workload, that change alone gives βˆ’6.4 %, this one alone βˆ’9.4 %, and the two together βˆ’9.4 %. They attack the same enumerators from opposite ends, so the wins are not additive.

Activity

  1. added
    triage/untriaged
    on Sep 16, 2026
  2. Jen-Uno commented on Sep 30, 2026

    @Jen-Uno
    Contributor

    QA: closing out without a manual pass. The fix in #24519 ships with automated tests (Given_DependencyObjectCollection.cs) that exercise the change.

    If you still see this behaviour on a build that includes that PR, please reopen or comment here.

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