Skip to content

Hit-testing visits the entire visual tree on every pointer move (no subtree bounds pruning) #24815

Open

Description

@MartinZikmund

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().IsEnabled check
  • 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 / GenerateChildOuterBounds store the union of the children's outer bounds in m_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 InputInjector over a 300-item ListView.
  • 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 OnPointerMoved is 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_fOuterBoundsDirty flags are the reference.
  • Some cheaper per-node wins are also available, but they only reduce the constant factor:
    • skip the Log().IsEnabled(Trace) check inside GetTransform on the hot path
    • avoid transforming two rects per node

Activity

  1. added
    kind/enhancement
    triage/untriaged
    project/pointers 🖱️
    area/performance 📈
    on Sep 29, 2026
  2. MartinZikmund commented on Oct 14, 2026

    @MartinZikmund
    MemberAuthor

    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 GridView style on Skia (it falls back to WrapPanel since ItemsWrapGrid isn't implemented, see #468), 5,000 realized 120x120 tiles, Win32 Skia, synthetic wheel spin at 40 notches/s through InputInjector.

    • The wheel goes through InputManager.PointerManager.OnPointerWheelChanged -> VisualTreeHelper.SearchDownForTopMostElementAt, which was ~89% of UI-thread samples (EventPipe sample profiler).
    • The search visits every child of the WrapPanel and calls UIElement.GetTransform for each one, once per wheel event.
    • Old GridViewItem Grid/Rectangle template: 1.5 new frames/s, input-to-frame latency p50 4.8 s / p95 9.1 s.
    • ListViewItemPresenter template (cheaper per-item HitTest): 6.6 frames/s, p50 ~0.2 s.
    • A single hover move costs 15-25 ms of UI-thread time.
    • Re-measured on 2026-10-13 on a dev.2275 build: zero new frames during the 8 s spin (one at the end), ~9.8 s of UI time. Still the same hot path.

    Still true on current master: SearchDownForTopMostElementAt only 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 (OnPointerMoved is expensive), #25269 (FindElementsInHostCoordinates ignores clips).

  3. added
    7.1
    on Oct 14, 2026
  4. removed
    7.1
    on Oct 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area/performance 📈kind/enhancementproject/pointers 🖱️triage/untriaged

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions