Repository navigation
perf(uno-ui): Index DO collections in tree walks - #24519
Conversation
The Enter, Leave and resource-binding walks visited the collections a property holds through IEnumerable, boxing a List<T>.Enumerator per collection per visit. Every VisualStateGroup, VisualState, Setter, StateTrigger and VisualTransition collection of a templated control paid it on each Enter, Leave and theme update, as did the VisualStateGroup[] emitted by XAML codegen, Storyboard children and TextBlock inlines. DependencyObjectCollectionBase now exposes its backing list as an indexable IList, and the walks enumerate through DependencyObjectItems, which indexes framework DO collections and arrays and falls back to the value's own enumerator for anything else. The collection bumps a version on each mutation and the walker checks it, so a collection modified mid-walk still throws as List<T>.Enumerator did. WinUI instead locks the collection for the duration of the walk (CDOCollection::EnterImpl); that changes which caller throws, so it is left for its own change. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_012QzKQawubFcPPX9JSoZLkX
|
Claude finished @MartinZikmund's task in 3m 17s —— View job Review complete ✅Todo list:
Verdict: ship (1 nit)Findings by severity: 0 blocking, 0 major, 1 minor (posted inline). What I checked
Cross-cutting notes
|
There was a problem hiding this comment.
🟡 Changes recommended
Mutation coverage and two test documentation/annotation nits remain unresolved.
Get a fresh assessment by requesting another Copilot review.
Pull request overview
This PR reduces enumerator allocations during Uno UI tree and theme walks while preserving mutation detection behavior.
Changes:
- Adds indexed traversal for dependency-object collections and arrays.
- Optimizes Enter/Leave, theming, and
TextBlockinline walks. - Adds regression tests for themed collection items.
File summaries
| File | Reviewed changes and findings |
|---|---|
src/Uno.UI/UI/Xaml/Documents/InlineCollection.cs |
Exposes allocation-free inline enumeration. No additional findings. |
src/Uno.UI/UI/Xaml/DependencyObjectCollection.cs |
Adds indexed traversal and mutation tracking. Moderate finding (1 vote): add mutation-during-walk regression coverage. |
src/Uno.UI/UI/Xaml/DependencyObject.Theming.cs |
Uses indexed theme-resource traversal. No additional findings. |
src/Uno.UI/UI/Xaml/DependencyObject.PropertySystem.mux.cs |
Uses indexed Enter/Leave walks. No additional findings. |
src/Uno.UI/UI/Xaml/Controls/TextBlock/TextBlock.cs |
Uses the allocation-free inline enumerator. No additional findings. |
src/Uno.UI.RuntimeTests/Tests/Windows_UI_Xaml/Given_DependencyObjectCollection.cs |
Adds themed collection tests. Nits (3 votes each): fix the dangling cref and add GitHubWorkItem annotations to the issue-specific tests. |
Review details
Suppressed comments (2)
src/Uno.UI.RuntimeTests/Tests/Windows_UI_Xaml/Given_DependencyObjectCollection.cs:101
- This second regression test also guards issue #24517, but it is not linked with
GitHubWorkItem, unlike issue-specific runtime tests elsewhere. Add the same issue annotation here so the test remains traceable when it fails.
[TestMethod]
src/Uno.UI/UI/Xaml/DependencyObjectCollection.cs:33
- The new
ItemsVersionpath is the only thing preserving the old fail-fast behavior after replacingList<T>.Enumeratorwith indexing, but the added tests never mutate a walkedDependencyObjectCollection. Add regression coverage that changes the collection from a walk callback and verifies Enter/Leave handle the resultingInvalidOperationExceptionas before while the theme walk still propagates it; otherwise a future mutation path can omit this bump and silently alter traversal.
private protected void OnItemsMutated() => ItemsVersion++;
- Files reviewed: 6/6 changed files
- Comments generated: 2
- Review effort level: Lite
💡 Add a code-review agent skill for context-aware, tailored reviews. Learn more in the docs.
|
🤖 Your WebAssembly Skia Sample App stage site is ready! Visit it here: https://unowasmprstaging.z20.web.core.windows.net/pr-24519/wasm-skia-net9/index.html |
|
The build 233976 found UI Test snapshots differences: Details
|
morning4coffe-dev
left a comment
There was a problem hiding this comment.
Reviewed the exact current head. Indexed DependencyObject collection access preserves traversal order and collection semantics while removing repeated enumeration overhead. No significant issue found.
|
Tick the box to add this pull request to the merge queue (same as
|
GitHub Issue: closes #24517
PR Type:
🔄 Refactoring (no functional changes, no api changes)
What changed? 🚀
The Enter, Leave and resource-binding walks visited the collections a property holds through
IEnumerable, so each visit boxed aList<T>.Enumerator(40 B). EveryVisualStateGroup,VisualState,Setter,StateTriggerandVisualTransitioncollection of every templated control paid it on each Enter, each Leave and each theme update — in a virtualized list, on every recycled container.VisualStateManager.VisualStateGroups, which XAML codegen stores as aVisualStateGroup[], boxed an array enumerator on the same walks, andTextBlock.UpdateThemeBindingsboxed one perInlineCollection.DependencyObjectCollectionBasenow carries the backing list as an indexableIList, and the six walk sites enumerate through oneDependencyObjectItemsref struct that indexes framework DO collections and arrays, and falls back to the value's own enumerator for anything else.TextBlockuses the struct enumeratorInlineCollectionalready had.Two details worth calling out:
List<T>.Enumerator— including the Enter/Leavecatchthat swallows it, and the theming walk where it propagates. WinUI insteadLock()s the collection during the walk (CDOCollection::EnterImpl); adopting that would change which code throws, so it belongs in its own change.EnterImpl/LeaveImplwalks its items; in Uno the owner still drives the walk (the existingTODO UnoinDependencyObject.PropertySystem.mux.cs). This change only removes the boxing from the owner-driven walk; the seam it adds is the data source a future port would reuse.Measured impact 📊
Managed bytes allocated per iteration on the UI thread (
GC.GetAllocatedBytesForCurrentThread, median of 7 rounds, two runs per side; reproduces within ~1 %):ButtonCheckBoxToggleSwitchListViewItemTextBoxListView(2,000 items) scroll stepEvery saving is an exact multiple of 40 B, i.e. exactly the enumerator boxes and nothing else.
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 this machine, far above any effect. Gen0 counts were captured too, but at 0–2 per round they are too coarse to show a difference.Overlap with #24235. On the scroll workload that PR alone gives −6.4 %, this one alone −9.4 %, and both together −9.4 % — measured on a tree with #24235 cherry-picked. They remove the same enumerators from opposite ends, so the wins are not additive and this PR subsumes it on that workload.
Validation ✅
Runtime tests, Skia Desktop —
Given_VisualStateManager,Given_ThemeResource,Given_ElementTheme,Given_ElementTheme_Resolution_Regression,Given_MergedAppResources_ThemeResource,Given_Theme_Materialization,Given_FrameworkElement_ThemeResources,Given_DependencyObjectCollection,Given_Style,Given_ResourceDictionary,Given_Storyboard,Given_TextBlock,Given_Run,Given_Hyperlink,Given_Button,Given_CheckBox,Given_CheckBox_ThemeResource_Regression,Given_ToggleSwitch,Given_ListViewBase: 506 run, 498 passed, 15 skipped, 1 inconclusive, 7 failed.The failures are the
Given_ListViewBaselayout-rounding/timeout set plusGiven_TextBlock.When_Inlines_Transitively_Change, all of which fail identically on a build of the merge-base without this change (verified by rebuilding and rerunning just those tests).Two runtime tests were added to
Given_DependencyObjectCollection, covering theme-bound values that live inside collections the walks visit (GradientStopson a brush, aRuninTextBlock.Inlines): one for a subtree that leaves and re-enters the tree under the other theme, one for a subtree that joins the tree with a theme already set.Both were checked against a deliberately broken walker (indexed path visiting no items):
When_Subtree_Reenters_Tree_Then_Items_In_Collections_Are_Updatedfails under it, so it genuinely guards this change;When_Themed_Subtree_Enters_Tree_Then_Items_In_Collections_Are_Resolvedstill passes, so it covers Enter-time theme resolution through another path rather than this walk. Both pass on a build of the merge-base, i.e. they assert behaviour this PR preserves rather than introduces.PR Checklist ✅
Screenshots Compare Test Runresults. — to check once CI has run🤖 Generated with Claude Code
https://claude.ai/code/session_012QzKQawubFcPPX9JSoZLkX
Internal tracking: unoplatform/uno-private#2349 (finding 7), under unoplatform/uno-private#2346.