-
Notifications
You must be signed in to change notification settings - Fork 81
Avoid busy waiting on the main and the midi threads. #405
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
|
I have seen this and plan on going through it after the next release is out (any day now). Thanks for the suggestions and PR. :) |
|
Ooh, this is pretty great. With this applied, CPU usage went from 110% down to 6% with vsync enabled. |
|
@mlauss2 Let me rebase, running your CPU at 110% long term is not a good idea!!! |
|
@mlauss2 I've rebased. |
|
I tried the frame limiter changes now as well. When not moving mouse/using keyboard, the framerate is about 5% off, when moving the character it goes up to 120, and the whole thing is really choppy when target framerate is set to 30fps. I suspect that using SDL_WaitEventTimeout() is an unsuitable wait function since it advances the game prematurely whenever input activity is detected, which the frame limiter in its current design wants to actually prevent. I'd say drop the frame limiter changes for now; the midi thread parts are great and should go in asap as they make a world of difference wrt. cpu usage. |
|
Thanks for all your testing!
Yeah that limiter was not that good, let's kick it out. |
|
@MIvanchev please redo the patch with the frame limiter bits removed. Thanks! |
|
I haven't forgotten @mlauss2, just need some time to do it :) Let me get to it ASAP. |
|
I've changed the code a bit, maybe you friends can check! |
|
gcc says: same for next line with std::min(). I think gcc expects a comparison function as 2nd parameter. Why so complicated though, this here works well too, both clang and gcc are happy with it (no overflow/truncation warnings): |
|
I'm not sure if infinities and negative values are undefined behavior in that case, otherwise a good solution unless someone changes the types of timeStep/accumulator. In general, C++ has facilities to make the code a little bit safer but they don't seem to be well supported 🤭 |
|
OK the deduction should work now, it's hard to test for me RN cuz I'm on a very limited PC... it seems to work with GCC. |
Yes both clang and gcc are now happy. |
mlauss2
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking good, both clang and gcc are happy.
|
@luciusDXL please consider to include this PR into the next release. It makes a world of difference wrt. CPU usage. |
Solves #402.
Hi, I would like to suggest the following changes that dramatically improve the CPU usage by avoiding busy waiting in the threads. For the main thread I suggest using SDL_WaitEventTimeout in the frame limiting which pauses the thread until events arrive because I believe it's important to process events immediately. So if your mouse is, say, 1200Hz you will not be limited to 60FPS.
For the MIDI thread I propose a command condition which also awakes the thread immediately. Otherwise the thread is paused until it's time to play another note.
For me this brings down CPU usage from 30% to 10% and the fan stays off the whole time.