Skip to content

OnPointerMoved is expensive #12008

Open

Description

@Youssef1313

internal void OnPointerMoved(Windows.UI.Core.PointerEventArgs args)

This method is quite expensive. I captured a trace while keep moving the mouse over the samples list in samples app (was testing WPF)

Allocations:

image

All these happen in the method above.

Processing time:

image

7% of CPU time is spent there, which isn't good.

Activity

  1. added
    kind/enhancement
    triage/untriaged
    difficulty/tbd
    project/pointers 🖱️
    area/performance 📈
    on Apr 18, 2023
  2. self-assigned this
    on Apr 18, 2023
  3. Youssef1313 commented on Apr 18, 2023

    @Youssef1313
    MemberAuthor

    The boxing allocations of BubblingMode may be coming from HasFlag calls.

  4. Youssef1313 commented on Apr 18, 2023

    @Youssef1313
    MemberAuthor

    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.

  5. Youssef1313 commented on Jun 22, 2023

    @Youssef1313
    MemberAuthor

    Boxing allocations are likely to be addressed by #12677
    I'll look into the rest.

  6. MartinZikmund commented on Sep 29, 2026

    @MartinZikmund
    Member

    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:

    var clippingBounds = element.Visual.GetArrangeClipPathInElementCoordinateSpace() is { } clipping
        ? elementToRoot.Transform(clipping)
        : Rect.Infinite;

    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

    1. A small PR with the two allocation fixes above.
    2. Port WinUI's outer-bounds caching into SearchDownForTopMostElementAt. This is the main win and a larger change.
  7. MartinZikmund commented on Sep 29, 2026

    @MartinZikmund
    Member

    Split into separate issues:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

area/performance 📈difficulty/tbdkind/enhancementproject/pointers 🖱️triage/untriaged

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions