Repository navigation
Description
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_timerand sets it tonullScheduleTickNative(), 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
InputInjectorover a 300-itemListView. - 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
OnPointerMovedis expensive #12008. - Most of the remaining per-move allocations are the event args themselves:
PointerEventArgs,PointerPointandPointerPointPropertiesfrom the platform source, plusPointerRoutedEventArgsand thePointercreated inPointerRoutedEventArgs.GetPointer. - The
HasFlagboxing mentioned inOnPointerMovedis expensive #12008 is an artefact of profiling before the JIT has optimized the code: theHasFlagintrinsic only applies to fully optimized code. It doesn't happen in steady state.
Activity