-
Notifications
You must be signed in to change notification settings - Fork 13
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
Consider using std::hint::spin_loop, at least for Windows #11
Comments
Perhaps we could construct a scenario test that demonstrates hint being better to yield on Windows. If so I'd probably be happy to change that on that platform. Regarding Linux, I would say the better sleep accuracy just means we need to spin less. The point of the crate is avoiding oversleeping and that still applies. |
I made a crude benchmark that basically makes the worst-case scenario for yield. It generates all-core load on the computer, which will make (This will bring the computer to a crawl.) As expected,
While spin_loop hint will wake up on time in a lot of cases since the thread is running the least amount of time and most scheduler would prioritize such thread and allow preemption.
|
Thanks. From these results it does look worth switching to spin loop hint. I'll try running your test on Linux too. |
I've formalised some latency experiments in #12 they support that Windows would benefit greatly from using On Linux yielding seems to work well all the time. Do the changes in #12 look good to you? |
I'll publish a new version around the end of the week. |
The resolution of #1 was to use
yield_now
, which callsSleep(0)
on Windows. However, from https://randomascii.wordpress.com/2012/06/05/in-praise-of-idleness/:Not all of the above is relevant, but the point is that
Sleep(0)
either behaves as a syscall-inducing busy wait orSleep(1)
(and sometimes even worse). It's strictly worse than simply spinning, because syscalls takes longer time and if it transfers scheduling to another thread, then we'll be 100% missing the deadline.For Linux, spinning is questionable because hrtimer seems to actually provide sub-microsecond accuracy, and the wakeup delay is only affected by timer slack, which defaults to 50us. IMO we should not spin at all on Linux, but this would be a different discussion.
The text was updated successfully, but these errors were encountered: