You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
area/performance 📈Categorizes an issue or PR as relevant to performancedifficulty/tbdCategorizes an issue for which the difficulty level needs to be defined.kind/enhancementNew feature or requestproject/pointers 🖱️Categorizes an issue or PR as relevant to mouse/touch/pen pointerstriage/untriagedIndicates an issue requires triaging or verification
Reopening this: I re-measured on current master (f0adbbfd385, Skia). OnPointerMoved is still expensive, and most of the cost is in hit-testing.
Setup
A headless Skia app injects mouse moves (InputInjector) over a ListView with 300 items, similar to the samples list from the original report.
27 items are realized, with 14 elements each; the tree has 423 elements.
Allocations are measured with GC.GetAllocatedBytesForCurrentThread (median of 7 rounds).
The JIT is fully warmed up (DOTNET_TieredCompilation=0). CPU profiles come from EventPipe (dotnet-trace).
Workload
Bytes / move
CPU / move
Sweeping across items (3px steps)
653 B
~13 µs
Moving inside the top item (±1px)
712 B
~40 µs
Note
Profiles taken before the JIT warms up overstate the cost by about 3× (1.7 KB and ~150 µs per move here). That also explains the HasFlag boxing from the earlier comments: the HasFlag intrinsic only applies to optimized code, so the boxing disappears once methods are fully JIT-compiled. When profiling this, warm up first or disable tiered compilation.
1. Hit-testing visits the whole visual tree on every move (main cost)
About 86% of the CPU per move is in VisualTreeHelper.SearchDownForTopMostElementAt. Exclusive CPU:
SearchDownForTopMostElementAt: 31%
Matrix3x2Extensions.Transform: 22%
UIElement.GetTransform: 14%
Matrix4x4.Invert: 12%
The only thing that prunes a subtree is the element's clip:
Most elements have no clip, so clippingBounds is infinite. The search therefore descends into every subtree and rejects candidates only at the leaves, based on their rendering bounds. Each visited node pays for:
GetTransform(element, root)
two rect transforms
an inverse transform plus HitTest at leaves
Children are enumerated in reverse z-order. When the pointer is over the first item, nearly all ~400 elements are visited on every move. That's why hovering the top item costs about 3× more than the average sweep. The cost scales with the size of the visible tree and the complexity of templates.
WinUI avoids this. CUIElement::BoundsTestInternal checks each subtree against a cached outer bounds before descending:
EnsureOuterBounds / GenerateChildOuterBounds store the union of the children's outer bounds in m_outerBounds.
Dirty flags keep it up to date when layout or transforms change.
A subtree that the pointer can't touch is rejected with a single rect test. Porting that bounds caching would make hit-testing roughly proportional to tree depth instead of tree size. It would also help every other hit-test consumer: wheel, drag and drop, and FindElementsInHostCoordinates.
2. Avoidable allocations per move
DispatcherQueueTimer.Start() allocates a new System.Threading.Timer on each restart.
ScrollViewer.ShowScrollIndicator runs on every PointerMoved and calls _indicatorResetTimer.Start().
Restarting a running timer goes through StopNative(), which disposes the timer and sets it to null, then ScheduleTickNative(), whose _timer ??= new Timer(...) creates a new one.
That's Timer + TimerHolder + TimerQueueTimer + TimerCallback, about 300 B per move over any ScrollViewer.
The code is in DispatcherQueueTimer.others.cs, which exists in both Uno.UI.Dispatching and Uno.WinRT.
Fix: Change(Infinite, Infinite) in StopNative and keep reusing the same timer.
DirectManipulationCollection.Get allocates a closure and a Func on every move (about 88 B).
It's implemented as FirstOrDefault(manip => manip.IsTracking(identifier)).
Fix: a plain foreach loop.
A/B with both fixes applied locally (identical results across 3 alternating runs):
Workload
master
with fixes
Sweeping across items
653 B
518 B (−21%)
Moving inside one item
712 B
416 B (−42%)
CPU time doesn't change measurably with these fixes, because hit-testing dominates.
Most of what's left is the event args themselves:
PointerEventArgs, PointerPoint and PointerPointProperties, which come from the platform source
PointerRoutedEventArgs
the Pointer created in PointerRoutedEventArgs.GetPointer
Next steps
A small PR with the two allocation fixes above.
Port WinUI's outer-bounds caching into SearchDownForTopMostElementAt. This is the main win and a larger change.
area/performance 📈Categorizes an issue or PR as relevant to performancedifficulty/tbdCategorizes an issue for which the difficulty level needs to be defined.kind/enhancementNew feature or requestproject/pointers 🖱️Categorizes an issue or PR as relevant to mouse/touch/pen pointerstriage/untriagedIndicates an issue requires triaging or verification
Activity
Youssef1313 commentedon Apr 18, 2023
The boxing allocations of
BubblingModemay be coming fromHasFlagcalls.Youssef1313 commentedon Apr 18, 2023
Boxing in HasFlag was supposed to be eliminated in .NET Core 2.1.
I'm not sure why I'm seeing that in the trace when running on .NET 7.
It might be something other than HasFlag, I'll double check.
Youssef1313 commentedon Jun 22, 2023
Boxing allocations are likely to be addressed by #12677
I'll look into the rest.
MartinZikmund commentedon Sep 29, 2026
Reopening this: I re-measured on current
master(f0adbbfd385, Skia).OnPointerMovedis still expensive, and most of the cost is in hit-testing.Setup
InputInjector) over aListViewwith 300 items, similar to the samples list from the original report.GC.GetAllocatedBytesForCurrentThread(median of 7 rounds).DOTNET_TieredCompilation=0). CPU profiles come from EventPipe (dotnet-trace).Note
Profiles taken before the JIT warms up overstate the cost by about 3× (1.7 KB and ~150 µs per move here). That also explains the
HasFlagboxing from the earlier comments: theHasFlagintrinsic only applies to optimized code, so the boxing disappears once methods are fully JIT-compiled. When profiling this, warm up first or disable tiered compilation.1. Hit-testing visits the whole visual tree on every move (main cost)
About 86% of the CPU per move is in
VisualTreeHelper.SearchDownForTopMostElementAt. Exclusive CPU:SearchDownForTopMostElementAt: 31%Matrix3x2Extensions.Transform: 22%UIElement.GetTransform: 14%Matrix4x4.Invert: 12%The only thing that prunes a subtree is the element's clip:
Most elements have no clip, so
clippingBoundsis infinite. The search therefore descends into every subtree and rejects candidates only at the leaves, based on their rendering bounds. Each visited node pays for:GetTransform(element, root)HitTestat leavesChildren are enumerated in reverse z-order. When the pointer is over the first item, nearly all ~400 elements are visited on every move. That's why hovering the top item costs about 3× more than the average sweep. The cost scales with the size of the visible tree and the complexity of templates.
WinUI avoids this.
CUIElement::BoundsTestInternalchecks each subtree against a cached outer bounds before descending:EnsureOuterBounds/GenerateChildOuterBoundsstore the union of the children's outer bounds inm_outerBounds.A subtree that the pointer can't touch is rejected with a single rect test. Porting that bounds caching would make hit-testing roughly proportional to tree depth instead of tree size. It would also help every other hit-test consumer: wheel, drag and drop, and
FindElementsInHostCoordinates.2. Avoidable allocations per move
DispatcherQueueTimer.Start()allocates a newSystem.Threading.Timeron each restart.ScrollViewer.ShowScrollIndicatorruns on everyPointerMovedand calls_indicatorResetTimer.Start().StopNative(), which disposes the timer and sets it tonull, thenScheduleTickNative(), whose_timer ??= new Timer(...)creates a new one.Timer+TimerHolder+TimerQueueTimer+TimerCallback, about 300 B per move over anyScrollViewer.DispatcherQueueTimer.others.cs, which exists in bothUno.UI.DispatchingandUno.WinRT.Change(Infinite, Infinite)inStopNativeand keep reusing the same timer.DirectManipulationCollection.Getallocates a closure and aFuncon every move (about 88 B).FirstOrDefault(manip => manip.IsTracking(identifier)).foreachloop.A/B with both fixes applied locally (identical results across 3 alternating runs):
CPU time doesn't change measurably with these fixes, because hit-testing dominates.
Most of what's left is the event args themselves:
PointerEventArgs,PointerPointandPointerPointProperties, which come from the platform sourcePointerRoutedEventArgsPointercreated inPointerRoutedEventArgs.GetPointerNext steps
SearchDownForTopMostElementAt. This is the main win and a larger change.MartinZikmund commentedon Sep 29, 2026
Split into separate issues:
DispatcherQueueTimerrestart,DirectManipulationCollection.Get).