Skip to content

Special case runtime async in ThreadPool.UnsafeQueueUserWorkItem<TState>#127981

Draft
jakobbotsch wants to merge 3 commits intodotnet:mainfrom
jakobbotsch:queue-runtime-async-callback
Draft

Special case runtime async in ThreadPool.UnsafeQueueUserWorkItem<TState>#127981
jakobbotsch wants to merge 3 commits intodotnet:mainfrom
jakobbotsch:queue-runtime-async-callback

Conversation

@jakobbotsch
Copy link
Copy Markdown
Member

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.

This PR and #127973 closes the RPS gap between async1 and runtime async on ASP.NET platform-json.

…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.
@dotnet-policy-service
Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @JulieLeeMSFT, @VSadov
See info in area-owners.md if you want to be subscribed.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

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 ThreadPool callback (s_dispatchRuntimeAsyncContinuationsCallback) intended to dispatch runtime-async continuations.
  • Add a corresponding special-case in UnsafeQueueUserWorkItem<TState> to enqueue the Task state directly when that callback is used.
  • Update runtime-async ValueTaskSourceNotifier.OnCompleted wiring to use the new ThreadPool callback, 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.
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.

2 participants