We are currently migrating Bugzilla to GitHub issues.
Any changes made to the bug tracker now will be lost, so please do not post new bugs or make changes to them.
When we're done, all bug URLs will redirect to their equivalent location on the new bug tracker.

Bug 5171

Summary: PollEvent impacts performance in 2.0.12
Product: SDL Reporter: Carlos Tobon <jinzo2727>
Component: joystickAssignee: Sam Lantinga <slouken>
Status: RESOLVED FIXED QA Contact: Sam Lantinga <slouken>
Severity: major    
Priority: P2 CC: cameron.gutman, dl, stfx
Version: 2.0.12Keywords: target-2.0.14
Hardware: x86_64   
OS: Windows 10   
Attachments: Screen of profiling on SDL 2.0.12
Screen of profiling on SDL 2.0.8
More profile info

Description Carlos Tobon 2020-06-01 14:18:37 UTC
While working on a side project emulator, I noticed slow fps whenever using the PollEvent functions. I found someone having the same issue here on stackeoverflow:
 https://stackoverflow.com/questions/58639564/why-is-sdl-pollevent-so-slow/62103973#62103973 

I verified this when profiling the application using VS 2019 on Windows 10, with most of the cpu time being spent in the PollEvent function. Following some suggestions in the comments I downgraded to 2.0.8 and the issue was resolved.

I only tested versions 2.0.9 and 2.0.12 and noticed that the issue is still present.
Comment 1 Cameron Gutman 2020-06-04 05:28:13 UTC
Do you have sample code that reproduces the issue?

Can you post your profiling results?

SDL_PollEvent() is going to be hot in profiles, especially if called in a tight loop like that, because that's where most of the event handling logic of SDL takes place. To get deeper than just SDL exports in your profile, you'll probably need to build SDL2.dll locally to ensure the PDB is available for the profiler.
Comment 2 Carlos Tobon 2020-06-04 20:07:54 UTC
Created attachment 4370 [details]
Screen of profiling on SDL 2.0.12
Comment 3 Carlos Tobon 2020-06-04 20:08:42 UTC
Created attachment 4371 [details]
Screen of profiling on SDL 2.0.8
Comment 4 Carlos Tobon 2020-06-04 20:14:31 UTC
Hope this gives a better picture. Unfortunately this is what I can manage atm without building from src. 

If need be, I will try and see if I can do that over the weekend. Just want to point out that it becomes really fast when using 2.0.8 with no changes in the logic. and is broken from 2.0.9 and up.
Comment 5 Cameron Gutman 2020-06-07 01:58:28 UTC
Unfortunately, without symbols for SDL it's impossible to determine from the profiling data where the problem is inside SDL_PollEvent().

Fortunately, SDL has pre-made VS projects which make it pretty simple to build.
Comment 6 Carlos Tobon 2020-06-07 02:30:16 UTC
You're right, it was easy to build the project with the pdb. Just wanted to point out that in my particular scenario, I am using the c# bindings as well. 

Even with the pdbs for the bindings and SDL, I am not currently seeing any additional info when profiling. I do see that the symbols are being loaded. Do you have any other suggestions?
Comment 7 Carlos Tobon 2020-06-07 03:00:29 UTC
Created attachment 4374 [details]
More profile info

Additional profiling info
Comment 8 Carlos Tobon 2020-06-07 03:02:38 UTC
Please see the latest attachment as I was able to drill down and it looks to be an issue with the GetClipCursor() in WIN_UpdateClipCursor(). Let me know if you need any other additional info.
Comment 9 Dom 2020-06-07 07:05:13 UTC
This seems to be Bug 4748.

In SDL 2.0.12 this was tried to be fixed with https://hg.libsdl.org/SDL/rev/d5109d6a9b98 and https://hg.libsdl.org/SDL/rev/529fc1a611e8 which apparently didn't work. The original issue is caused by https://hg.libsdl.org/SDL/rev/2a9b4339468b in SDL 2.0.9.

This is a deal breaker for me and many other people being forced to downgrade to SDL 2.0.8 again if they want to use relative mouse mode or mouse grab without performance issues.

Please add this bug to target-2.0.14
Comment 10 Sam Lantinga 2020-06-10 04:48:42 UTC
Fixed, thanks!
https://hg.libsdl.org/SDL/rev/ff702ce6ab48
Comment 11 Carlos Tobon 2020-06-10 12:07:15 UTC
Glad I was able to help! Thanks!
Comment 12 Dave Lawrence 2020-08-09 11:31:06 UTC
Hi,
We are seeing occasional unexpected 'long' pauses in SDL_PollEvent, we added a very basic timer around the call to try and catch these, reporting execution times longer than 10ms, here's a short snippet of output:

2020-08-07 17:10:38 poll() SDL_PollEvent return 0, event type 0 - limit 10ms - duration 16ms
2020-08-07 17:10:52 poll() SDL_PollEvent return 0, event type 0 - limit 10ms - duration 15ms
2020-08-07 17:10:56 poll() SDL_PollEvent return 0, event type 0 - limit 10ms - duration 16ms
2020-08-07 17:10:58 poll() SDL_PollEvent return 0, event type 0 - limit 10ms - duration 15ms
2020-08-07 17:11:02 poll() SDL_PollEvent return 0, event type 0 - limit 10ms - duration 15ms
2020-08-07 17:11:04 poll() SDL_PollEvent return 0, event type 0 - limit 10ms - duration 16ms
2020-08-07 17:11:10 poll() SDL_PollEvent return 0, event type 0 - limit 10ms - duration 16ms
2020-08-07 17:11:17 poll() SDL_PollEvent return 0, event type 0 - limit 10ms - duration 16ms

... so every few seconds, but not at regular intervals.
Could this be the same issue as reported here?

(Windows10/SDL2.0.12)