Skip to content

Replace Task.Delay, which has only 15ms or so resolution, with an alternative, high-performance timer #17

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

Merged
merged 3 commits into from
Oct 13, 2022

Conversation

abelbraaksma
Copy link
Member

@abelbraaksma abelbraaksma commented Oct 13, 2022

Fixes #15.

It appears that the issue in #15 is caused by how Task.Delay is highly imprecise (with, depending on system, a 15ms resolution). Replacing it with what seems to be a possible high-resolution alternative (see this IoT helper code: dotnet/iot#235), which uses SpinWait and SpinOnce to delay the task, the "performance" issues disappeared as snow in the sun.

The expected timings of these smoke-tests should be roughly the amount of tasks divided by the average random delay configured for each task (this is a test feature only!).

It brings the offending tests from 15s runtime or more down to 600ms, with double the amount of test-tasks.

There are still some questions, mainly why does the spinwait.SpinOnce cause a 15ms delay per task on average?

TLDR: we now have a microsecond task-delay timer 😆!

@abelbraaksma abelbraaksma force-pushed the add-better-task-delay branch from f1defc3 to 20a4ac3 Compare October 13, 2022 01:21
@abelbraaksma abelbraaksma changed the title Replace Task.Delay, which has only 15ms or so resolution, with an alternative, high-performance times Replace Task.Delay, which has only 15ms or so resolution, with an alternative, high-performance timer Oct 13, 2022
@abelbraaksma
Copy link
Member Author

There are still some questions, mainly why does the spinwait.SpinOnce cause a 15ms delay per task on average?

About that: it turns out that SpinOnce internally uses the same timer as Task.Delay, and that it uses Thread.Sleep(0) to yield and Thread.Sleep(1) to block every couple of spins.

@abelbraaksma abelbraaksma merged commit 8b34b9d into main Oct 13, 2022
@abelbraaksma abelbraaksma deleted the add-better-task-delay branch October 13, 2022 12:34
@abelbraaksma abelbraaksma added this to the v0.1.0 milestone Mar 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Performance smoke-tests seem to be broken
1 participant