Skip to content

Avoidable allocations on every pointer move (DispatcherQueueTimer restart, DirectManipulationCollection.Get) #24816

Open

Description

@MartinZikmund

What would you like to be added? 💡

Remove two avoidable allocations that happen on every pointer move.

1. DispatcherQueueTimer.Start() allocates a new System.Threading.Timer each time a running timer is restarted.

ScrollViewer.ShowScrollIndicator runs on every PointerMoved and calls _indicatorResetTimer.Start(). Restarting a running timer goes through:

  • StopNative(), which disposes _timer and sets it to null
  • ScheduleTickNative(), whose _timer ??= new Timer(...) creates a new one

Each move over any ScrollViewer therefore allocates a Timer, TimerHolder, TimerQueueTimer and TimerCallback, about 300 B. The code is in DispatcherQueueTimer.others.cs, which exists in both Uno.UI.Dispatching and Uno.WinRT.

Suggested fix: keep the timer instance and have StopNative call _timer?.Change(Timeout.InfiniteTimeSpan, Timeout.InfiniteTimeSpan) instead of disposing it.

2. DirectManipulationCollection.Get allocates a closure and a Func on every pointer event.

BeforeMoveTryRedirectToManipulations calls Get on every move. Get is:

_instances.FirstOrDefault(manip => manip.IsTracking(identifier))

The lambda captures identifier, which costs about 88 B per move. Suggested fix: a plain foreach loop. OfType() captures the same way, but it only runs on enter and press.

Why is this needed? 🤔

Pointer moves arrive at up to 1 kHz. Measured on master (f0adbbfd385):

  • Headless Skia, mouse moves injected with InputInjector over a 300-item ListView.
  • The JIT is fully warmed up (DOTNET_TieredCompilation=0).
  • Allocations are measured with GC.GetAllocatedBytesForCurrentThread, median of 7 rounds.
Workload master with both fixes
Sweeping across items 653 B / move 518 B / move (−21%)
Moving inside one item 712 B / move 416 B / move (−42%)

Results were identical across 3 alternating A/B runs. CPU time doesn't change measurably, because hit-testing dominates; that's tracked separately.

Renderer 🎨

  • Skia
  • Native

For which platforms 📱💻🖥️

All platforms 🌍

Anything else we need to know? 💬

  • Split out of OnPointerMoved is expensive #12008.
  • Most of the remaining per-move allocations are the event args themselves: PointerEventArgs, PointerPoint and PointerPointProperties from the platform source, plus PointerRoutedEventArgs and the Pointer created in PointerRoutedEventArgs.GetPointer.
  • The HasFlag boxing mentioned in OnPointerMoved is expensive #12008 is an artefact of profiling before the JIT has optimized the code: the HasFlag intrinsic only applies to fully optimized code. It doesn't happen in steady state.

Activity

  1. added
    kind/enhancement
    triage/untriaged
    project/pointers 🖱️
    area/performance 📈
    on Sep 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