Repository navigation
Description
What would you like to be added? 💡
Prune whole subtrees during hit-testing by caching each element's outer bounds, the way WinUI does. Today VisualTreeHelper.SearchDownForTopMostElementAt visits essentially the whole visible visual tree on every hit test.
The only thing that can prune a subtree today is the element's clip:
var clippingBounds = element.Visual.GetArrangeClipPathInElementCoordinateSpace() is { } clipping
? elementToRoot.Transform(clipping)
: Rect.Infinite;Most elements have no clip, so clippingBounds is infinite. The search then descends into every subtree and only rejects candidates at the leaves, using their rendering bounds. Every visited node pays for:
UIElement.GetTransform(element, root)- two rect transforms
- a
Log().IsEnabledcheck - at leaves, an inverse transform plus
HitTest
Children are enumerated in reverse z-order. So when the pointer is over the first item of a list, nearly every element of every realized item is visited.
WinUI avoids this in CUIElement::BoundsTestInternal (dxaml/xcp/core/core/elements/uielement.cpp). Before descending, it tests the point against a cached outer bounds:
EnsureOuterBounds/GenerateChildOuterBoundsstore the union of the children's outer bounds inm_outerBounds.- Dirty flags invalidate it when layout, transforms or children change.
A subtree the pointer can't touch is rejected with a single rect test. Porting this would make hit-testing cost roughly proportional to tree depth instead of tree size.
Why is this needed? 🤔
Hit-testing runs on every pointer move, and it is the dominant CPU cost of pointer input on Skia. This is the main remaining cause of #12008.
Measured on master (f0adbbfd385) with the JIT fully warmed up (DOTNET_TieredCompilation=0):
- Headless Skia, mouse moves injected with
InputInjectorover a 300-itemListView. - 27 items are realized, with 14 elements each; the tree has 423 elements.
| Workload | CPU per move |
|---|---|
| Sweeping across items (3px steps) | ~13 µs |
| Moving inside the top item (±1px) | ~40 µs |
An EventPipe CPU profile attributes ~86% of the time per move to SearchDownForTopMostElementAt. Exclusive CPU:
SearchDownForTopMostElementAt: 31%Matrix3x2Extensions.Transform: 22%UIElement.GetTransform: 14%Matrix4x4.Invert: 12%
The cost grows with the number of realized items and the complexity of their templates. Other hit-test consumers pay it too: the wheel, drag and drop (DropUITarget), FindElementsInHostCoordinates, and the re-hit-tests after leave/enter changes the tree.
Renderer 🎨
- Skia
- Native
For which platforms 📱💻🖥️
All platforms 🌍
Anything else we need to know? 💬
- Split out of
OnPointerMovedis expensive #12008. Its separate allocation findings are tracked in a sibling issue. - Invalidation is the tricky part. Anything that changes an element's arranged rect, render transform, projection or clip, or its children, must dirty the element's own bounds and its ancestors' child bounds. WinUI's
InvalidateChildBounds/m_fChildBoundsDirty/m_fOuterBoundsDirtyflags are the reference. - Some cheaper per-node wins are also available, but they only reduce the constant factor:
- skip the
Log().IsEnabled(Trace)check insideGetTransformon the hot path - avoid transforming two rects per node
- skip the
Activity
MartinZikmund commentedon Oct 14, 2026
A worst-case data point: with a non-virtualized panel, the missing pruning shows up as seconds per wheel notch, not just microseconds per move.
Setup: default
GridViewstyle on Skia (it falls back toWrapPanelsinceItemsWrapGridisn't implemented, see #468), 5,000 realized 120x120 tiles, Win32 Skia, synthetic wheel spin at 40 notches/s throughInputInjector.InputManager.PointerManager.OnPointerWheelChanged->VisualTreeHelper.SearchDownForTopMostElementAt, which was ~89% of UI-thread samples (EventPipe sample profiler).WrapPaneland callsUIElement.GetTransformfor each one, once per wheel event.GridViewItemGrid/Rectangle template: 1.5 new frames/s, input-to-frame latency p50 4.8 s / p95 9.1 s.ListViewItemPresentertemplate (cheaper per-itemHitTest): 6.6 frames/s, p50 ~0.2 s.Still true on current master:
SearchDownForTopMostElementAtonly prunes with the clip/viewport rect (clippingBounds,element.Viewport), so siblings nowhere near the pointer are all visited. A cheap per-element bounds rejection before descending would turn this into one rect test per non-matching tile.Related: #12008 (
OnPointerMovedis expensive), #25269 (FindElementsInHostCoordinatesignores clips).