2
votes

Ive been programming an emulator as a side project and am at a point now where I am tackling efficiency. Ive managed to get rid of all the slow things that I was doing like lots of SDL_RenderDrawPoint's, but now I'm stuck.

Ive managed to narrow it down to SDL_PollEvent. Ive simplified my main loop and event loop so that they look similar to a simple SDL tutorial. When I comment out the event loop, the emulator runs super fast. Ive also used the visual studio performance profiler to check, and sure enough, SDL_PollEvent is taking up ~94% of the cpu time.

slow SDL_PollEvent

The place where my emulator does all its stuff is in ppuNptr->cycle() which is taking up only 1.48% .

Unfortunately I can't replicate this in another project so I'm at a loss.

Is there something that I should be doing before polling for events?

How else could I debug this?

Currently im on windows 10 with visual studio 2019 and sdl 2.0.10 .

2
Can you sample the events your application is receiving? - Botje
Most of the events "returned" by SDL_PollEvent won't be the ones you check for, which means the loop will iterate a lot and call SDL_PollEvent quite a lot. - Some programmer dude
Part of any game loop. Along with the renderer that presents frames and the game object updater that makes stuff move. When the latter two don't do anything important then all cpu time goes into the event poller. Which tells you that you're basically done optimizing. - Hans Passant
Need more info. What perf levels and drop we're talking about? Similar problem was reported on windows 10 and SDL 2.0.9, but that should be long fixed. You can build debug SDL to see its internals in debugger. - keltar
@Tristus as a quick test you can try 2.0.8 - keltar

2 Answers

2
votes

I too ran across this when profiling my emulator. The answer for me was to downgrade the version of SDL2 to 2.0.8.

I was running SDL2 version 2.0.12 and I took keltar's suggestion in the comments, and downgraded to 2.0.8 and I was no longer having the issue with the SDL_PollEvent causing slow performance. This fixed the issue for me as I no longer having low FPS issues with my emulator.

I will see if this issue has been reported in the bug ticketing system and report it if not to the SDL2 as this issue starts occurring from version 2.0.9.

0
votes

My solution to this was to move the event handling code out from the outer portion of the main loop (which runs at 10Mhz; that’s 100 nanoseconds per loop!) and moved it into my rendering code instead, which only ran every 16ms for 60fps. I was able to get my emulator from 0.1Mhz to 10Mhz this way.

Looking back, I guess it was unreasonable to expect SDL_PollEvent to run in only a few nanoseconds. (Though XCB was able to achieve it.)