Skip to content
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

Update increaseticks sleep strategy #2655

Merged
merged 1 commit into from Jul 3, 2023
Merged

Conversation

kklobe
Copy link
Collaborator

@kklobe kklobe commented Jun 29, 2023

This is an experiment to improve the accuracy of the engine that is increaseticks. PIC_RunQueue is at the mercy of the 1ms sleep_for, which can cause an entire millisecond tick to be missed. By lowering the sleep time, PIC_RunQueue can run more often. This allows it to get closer to the precise time specified when an event is added via PIC_AddEvent; many calls throughout the codebase provide sub-millisecond or fractional delays.

Also, it keeps track of the excess microseconds after each sleep and adjusts ticksDone for more accuracy during cycle guessing, since sleep_for isn't precise; for example, a 1ms sleep is actually ~1150-1200us on my M1 Mac, causing it to think it slept for a total of 10 ticks when it was really 11.

None of this should provide a massive difference, but it should give much more accurate responsiveness within ticks. I did notice a difference on the Future Crew Panic demo, on the first 'scrollers suck' portion, with fewer flickers compared to main. At best, it will help smooth out micro-glitches in audio and video here and there, and worst case it should be identical to main.

The one wild card is the new sleepDuration:

constexpr auto sleepDuration = std::chrono::microseconds(100);

On my M1 Mac, it significantly reduces CPU usage compared to 250 or 500 (and even 1000), but that is completely up to the Host OS scheduler. I wanted to start with an aggressive value, and we can back it off if empirical testing warrants.

@kklobe kklobe requested a review from kcgen June 29, 2023 19:25
@johnnovak
Copy link
Member

Nice one @kklobe! Definitely a welcome improvement, I'm only hoping the current inaccurate sleep behaviour doesn't mask some timing related emulation deficiencies. But we will see!

Keep track of the leftover sleep microseconds so that several ticks
variables remain accurate over time.
@kklobe kklobe force-pushed the kk/increaseticks-experiment-1 branch from bdf8747 to 9c0a780 Compare July 1, 2023 22:14
@kklobe kklobe marked this pull request as ready for review July 1, 2023 22:20
@kklobe kklobe removed the request for review from kcgen July 1, 2023 22:20
Copy link
Member

@johnnovak johnnovak left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks ok, I guess we can just merge and live with it for a while and see what happens!

@FeralChild64 FeralChild64 added the enhancement New feature or enhancement of existing features label Jul 3, 2023
@johnnovak johnnovak merged commit 1eadb1c into main Jul 3, 2023
52 checks passed
@johnnovak johnnovak deleted the kk/increaseticks-experiment-1 branch July 8, 2023 00:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or enhancement of existing features
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

None yet

3 participants