Repository navigation
Description
Current behavior 🐛
Severity: High - Any search, filter or refresh UI that replaces ItemsSource while the user is scrolled down freezes the UI thread for seconds (4-17 s measured) and churns ~2 GB of managed heap, in proportion to how far the user had scrolled.
On Skia, replacing ListView.ItemsSource keeps the old scroll offset (WinUI resets to 0). The next layout pass then has to fill the panel from item 0 up to that offset, and that fill is O(n^2). Two problems, one trigger:
- Trigger: the scroll offset survives the swap. In a
ListView/GridViewscrolled to V=4000, assigning a new same-size list toItemsSource(orClear()+ refill, ornullthen a new list) leaves V=4000 and shows the new data scrolled down. Native WinUI 3 (WinAppSDK 2.5.1) goes back to V=0. Uno also realizes every container from 0 up to the offset (115 instead of ~51 in the 4000 px case):
ListView ItemsSource swap (same size): WinUI V=0 n=51 | Uno V=4000 n=115 (0..114); GridView 1500 kept
refill after ItemsSource=null: WinUI V=0 | Uno V=4000
- Cost: filling from item 0 is quadratic. With a 10,000-item
ListView(Height=600, default template, ~40 px rows) swapped while scrolled far down, the UI thread blocks:
| offset at swap | time for ItemsSource = ... + next layout |
managed heap |
|---|---|---|
| 0 | 21-50 ms | ~45 MB |
| ~29,800 | ~0.75 s | |
| 257,000-380,000 | 4-17 s | up to ~2 GB |
Heap returns to ~55 MB after a full GC, so this is transient churn rather than a leak, but a 400-cycle soak reached a 2-4.4 GB working set. ScrollIntoView on an unchanged source takes only 90-230 ms, and a swap at offset 0 is fast, so it takes both steps together.
Stacks taken from the UI thread during the stall:
ItemsStackPanel.MeasureOverride -> VirtualizingPanelLayout.UpdateLayout -> FillLayout -> FillForward -> AddLine -> GetItemsEnd()
AddLine -> AddView -> SelectorItem.MeasureOverride -> TextBlock.ParseText
Expected behavior 🎯
After ItemsSource changes, the ScrollViewer goes back to the top like on WinUI (backed by native WinUI 3 runs, WinAppSDK 2.5.1), so the fill only realizes about one viewport of containers and the swap costs about the same as at offset 0 (tens of ms).
How to reproduce it (as minimally and precisely as possible) 🔬
- Create a Skia desktop app and host this in a page (
rootis anyGrid):
var lv = new ListView { Height = 600 };
root.Children.Add(lv);
lv.ItemsSource = Enumerable.Range(0, 10000).Select(x => $"item {x}").ToList();
await Task.Delay(300);
var sw = Stopwatch.StartNew();
lv.ItemsSource = Enumerable.Range(0, 10000).Select(x => $"warm {x}").ToList();
await Task.Delay(5);
Debug.WriteLine($"swap at offset 0: {sw.ElapsedMilliseconds} ms"); // ~20-50 ms
lv.ScrollIntoView(lv.Items[7500]); // offset ~300,000 px
await Task.Delay(100);
sw.Restart();
lv.ItemsSource = Enumerable.Range(0, 10000).Select(x => $"new {x}").ToList();
await Task.Delay(5);
Debug.WriteLine($"swap while scrolled: {sw.ElapsedMilliseconds} ms"); // seconds- Run it and watch the UI thread: it hangs for several seconds, and
GC.GetTotalMemory(false)climbs to 1-2 GB. - Read
ScrollViewer.VerticalOffsetafter the swap: Uno keeps the old offset; WinUI reports 0.
Runtime-verified on master 4636b705e97 (Skia desktop, Windows, .NET 11.0.100): 8 cycles of "replace ItemsSource with a new 10,000-item list, then ScrollIntoView a random item". The swap at the start of each cycle took 50 ms in cycle 1 (offset 0), then 4496 ms (previous offset 378,920), 13251 ms (257,000), 7174 ms (347,000), 13185 ms (298,400), 10203 ms (29,800), 566 ms (311,080) and 11946 ms (50,160); the heap peaked at 2050 MB. The cost is not strictly monotonic in the offset. VirtualizingPanelLayout.managed.cs and ScrollViewer.cs are unchanged between that build and current master (46ea872727d). The WinUI numbers above come from native WinUI 3 (WinAppSDK 2.5.1) runs of the same scenario.
Workaround 🛠️
Scroll to the top before replacing ItemsSource, or mutate an ObservableCollection in place:
scrollViewer.ChangeView(null, 0, null, disableAnimation: true);
lv.ItemsSource = newItems;Renderer 🎨
- Skia
- Native
Affected platforms 📱💻🖥️
Skia desktop (Windows) was tested. The managed VirtualizingPanelLayout is shared by the Skia heads, so the others likely behave the same (not tested).
Uno.Sdk version (and other relevant versions) 📦
master 4636b705e97, net11.0-desktop, .NET SDK 11.0.100.
IDE version 🧑💻
N/A (command-line run).
Anything else we need to know? 💬
Root cause, by code inspection on 46ea872727d (the freeze is runtime-verified; why exactly the offset survives is not located):
- The offset survives the Reset. Nothing resets the
ListView's scroll position on anItemsSourcechange. TheScrollVieweroffset intent (_verticalOffsetIntent, from 385b614) may be what keeps it, so check whether fixing that alone fixes the swap. Nothing then seeds the fill near the kept offset. - The fill walks from item 0.
FillLayoutcallsFillForward, which keeps callingAddLineuntilGetItemsEnd()reachesExtendedViewportEnd, so it materializes and measures every item between 0 and the offset (~7,500 containers at 300k px):uno/src/Uno.UI/UI/Xaml/Controls/ListViewBase/VirtualizingPanelLayout.managed.cs
Lines 555 to 565 in 46ea872
void FillForward() { var seed = _dynamicSeedIndex; while ( GetItemsEnd() is var end && (end ?? 0) < ExtendedViewportEnd + extentAdjustment && GetNextUnmaterializedItem(Forward, seed ?? GetLastMaterializedIndexPath()) is { } nextItem) { AddLine(Forward, nextItem); seed = null; } } - The loop condition is a linear scan.
GetItemsEnd()iterates all of_materializedLineson every pass, so the fill is O(lines^2):uno/src/Uno.UI/UI/Xaml/Controls/ListViewBase/VirtualizingPanelLayout.managed.cs
Line 1169 in 46ea872
private double? GetItemsEnd()
Suggested fix: reset the offset on ItemsSource change like WinUI, or seed the fill at the offset (as the large-scroll path of OnScrollChanged does). Making GetItemsEnd O(1) alone still leaves the O(n) container churn.
Related: #25294 describes the linear-scan accessors from the code side without measurements; this issue is the first measured cost of them, plus the trigger. #25462 is a different symptom of an ItemsSource change while scrolled (ItemsView/LinedFlowLayout shrink case).
Activity