winit icon indicating copy to clipboard operation
winit copied to clipboard

Rapid cursor movement causes frame loss

Open AuracleTech opened this issue 9 months ago • 3 comments

Description

Faster cursor moves, the less framerate

No render load :

  • no cursor movement : ~4500 draw/s
  • rapid cursor movement : ~300 draw/s

Heavy render load :

  • no cursor movement : ~20 draw/s
  • rapid cursor movement : ~5 draw/s

Tried this pull request for high polling rate fix, didn't work Note that I do not even utilize inputs yet

my event loop source

Green = framerate with no cursor movement Red = framerate with rapid cursor movements

image

Windows version

Microsoft Windows 11 Pro
Version	10.0.22631 Build 22631

Winit version

0.29.15

AuracleTech avatar May 08 '24 02:05 AuracleTech

Seems like a strange issue. When recording, the bug is less visible. Tried changing Razer Synapse between 125hz and 8000hz, losing frames mostly at 8000hz. When OBS records I only lose 3-4 frames at 8000hz. Thinking it might be a Windows problem rather than winit. Profiling needed for confirmation.

AuracleTech avatar May 10 '24 07:05 AuracleTech

Conclusion: This is a Microsoft Windows issue, even without using winit the issue persists...

flamegraph

Note : function name are cryptic because they aren't from rust but from the Windows OS itself

image

Windows performance analyzer

No cursor movement :

  • 33 000 Thread: CSwitch
  • almost zero QueueNullPostMessage image

Cursor movement :

  • 225 000 Thread: CSwitch
  • 4000 QueueNullPostMessage which are the same length as the Thread: CSwitch so they must be related somehow image

Hot spot can be found in winit/src/platform_impl/windows/event_loop.rs line ~460 where a loop iterates. If you stopwatch, the whole loop takes 100 nanoseconds with the cursor at rest. But with rapid cursor movements it ramps up to 50 to 150 milliseconds, this is a massive performance hit.

Temporary fix 1 : Reduce polling rate of cursor to 1000 Hz or under Temporary fix 2 : Do not rely on WindowEvent::RedrawRequested or the event loop to render, also multithread the renderer

Hopefully this helps

AuracleTech avatar May 11 '24 04:05 AuracleTech

polling rate of cursor

Actually, I remember reading a (very good) article about this before, probably from Hacker News or something, can't seem to find it now. But a quick Google search does indeed suggest that this is a more general problem on Windows.

madsmtm avatar May 11 '24 20:05 madsmtm

I am closing this issue considering its a windows issue and there's nothing we can do except point fingers at @Microsoft

AuracleTech avatar Jul 19 '24 03:07 AuracleTech

I read a related blog post about this today, see https://ph3at.github.io/posts/Windows-Input/

madsmtm avatar Sep 18 '24 12:09 madsmtm