Special case runtime async in ThreadPool.UnsafeQueueUserWorkItem<TState>#127981
Draft
jakobbotsch wants to merge 3 commits intodotnet:mainfrom
Draft
Special case runtime async in ThreadPool.UnsafeQueueUserWorkItem<TState>#127981jakobbotsch wants to merge 3 commits intodotnet:mainfrom
ThreadPool.UnsafeQueueUserWorkItem<TState>#127981jakobbotsch wants to merge 3 commits intodotnet:mainfrom
Conversation
…ate>` When user code forwards continuations passed to `IValueTaskSource.OnCompleted` to the thread pool we can avoid allocating a new work item. This function already has a special case that implements this optimization for async1. Add one for runtime async too.
Contributor
|
Tagging subscribers to this area: @JulieLeeMSFT, @VSadov |
Contributor
There was a problem hiding this comment.
Pull request overview
This PR extends the existing ThreadPool.UnsafeQueueUserWorkItem<TState> fast-path for known async continuation callbacks to also cover runtime-async continuations, enabling user implementations of IValueTaskSource.OnCompleted to forward the provided continuation to the ThreadPool without allocating an extra work-item wrapper.
Changes:
- Add a new known
ThreadPoolcallback (s_dispatchRuntimeAsyncContinuationsCallback) intended to dispatch runtime-async continuations. - Add a corresponding special-case in
UnsafeQueueUserWorkItem<TState>to enqueue theTaskstate directly when that callback is used. - Update runtime-async
ValueTaskSourceNotifier.OnCompletedwiring to use the newThreadPoolcallback, and remove the previous per-type callback.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| src/libraries/System.Private.CoreLib/src/System/Threading/ThreadPoolWorkQueue.cs | Introduces the runtime-async known callback and a UnsafeQueueUserWorkItem<TState> special-case to avoid wrapper allocations. |
| src/coreclr/System.Private.CoreLib/src/System/Runtime/CompilerServices/AsyncHelpers.CoreCLR.cs | Switches runtime-async IValueTaskSource continuation registration to the new ThreadPool callback and deletes the old callback. |
Comment on lines
+1435
to
+1436
| // We know RuntimeAsyncTask overrides this and calls | ||
| // DispatchContinuation without looking at the Thread. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
When user code forwards continuations passed to
IValueTaskSource.OnCompletedto the thread pool we can avoid allocating a new work item. This function already has a special case that implements this optimization for async1. Add one for runtime async too.This PR and #127973 closes the RPS gap between async1 and runtime async on ASP.NET platform-json.