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

Fixed writing pages in the same thread that does EnqueuePage #2453

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

alexbereznikov
Copy link

AsyncManualResetEvent was using TaskCompletionSource which by default runs continuations synchronously. That led to writing pages in the same thread that called EnqueuePage and, as a result, ~4x performance degradation.

Used more correct way to asynchronously wait for wait handle.

This partially fixes #2451

@JKamsker
Copy link
Collaborator

JKamsker commented Jun 4, 2024

I am concerned about the extra dependencySystem.Threading.ThreadPool.
Will that be a problem somewhere?

Also, do you happen to have a Benchmark (result+code) at hand?

@alexbereznikov
Copy link
Author

Hi @JKamsker , this dependency exists only because of netstandard1.3 target (for some reason thread pool routines are not available out of box in netstandard1.3). I don't think it is a vulnerable package or something.

Benchmark code is contained in this bug - #2451 , as well as results in terms of time taken to run the app. This pr aimed to partially fix it as well

Copy link
Collaborator

@pictos pictos left a comment

Choose a reason for hiding this comment

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

Hey @alexbereznikov, thanks for this fix. I've a couple of suggestions.

And please, can you add unit tests for this, that way we can avoid regressions

{
public static async Task<bool> WaitAsync(this WaitHandle handle)
{
var tcs = new TaskCompletionSource<bool>();
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
var tcs = new TaskCompletionSource<bool>();
var tcs = new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously);

}
}

private sealed class ThreadPoolRegistration : IDisposable
Copy link
Collaborator

Choose a reason for hiding this comment

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

If we change this to be a struct does the code work as expected? If so, let's change it and implement the interface implicit, that way we don't need to box it.

Changing this to a struct will avoid allocations if the task is already completed

Suggested change
private sealed class ThreadPoolRegistration : IDisposable
private readonly struct ThreadPoolRegistration : IDisposable

{
_registeredWaitHandle = ThreadPool.RegisterWaitForSingleObject(handle,
(state, timedOut) => ((TaskCompletionSource<bool>)state).TrySetResult(!timedOut), tcs,
Timeout.InfiniteTimeSpan, executeOnlyOnce: true);
Copy link
Collaborator

Choose a reason for hiding this comment

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

do we want the Timeout to be infinite? I suggest using a large time, for example 1 day. You would be surprise for systems that get deadlocked because of infinite time spans

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[BUG] Performance degradation in v5.0.18
3 participants