Skip to content

[Runtime Async] Reuse ValueTaskSourceNotifier instance#126622

Open
VSadov wants to merge 2 commits intodotnet:mainfrom
VSadov:cacheNotifier
Open

[Runtime Async] Reuse ValueTaskSourceNotifier instance#126622
VSadov wants to merge 2 commits intodotnet:mainfrom
VSadov:cacheNotifier

Conversation

@VSadov
Copy link
Copy Markdown
Member

@VSadov VSadov commented Apr 7, 2026

This avoids allocation on every time we need to suspend on a ValueTaskSource.

Copilot AI review requested due to automatic review settings April 7, 2026 23:40
@dotnet-policy-service
Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @agocke
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

Reduces per-await allocations in runtime-async by reusing a per-thread ValueTaskSource notifier instead of allocating a new wrapper object each time a ValueTask suspends on an IValueTaskSource.

Changes:

  • Replace per-call ValueTaskSourceNotifier allocations with a [ThreadStatic] cached ValueTaskSourceNotifier instance.
  • Introduce a reusable notifier that stores the source, token, and an OnCompleted dispatcher delegate.
  • Update CoreCLR runtime-async suspension state to store the concrete ValueTaskSourceNotifier type (instead of a removed interface).

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/Tasks/ValueTask.cs Adds reusable ValueTaskSourceNotifier and updates AsTaskOrNotifier to return a cached notifier for IValueTaskSource-backed ValueTasks.
src/coreclr/System.Private.CoreLib/src/System/Runtime/CompilerServices/AsyncHelpers.CoreCLR.cs Updates runtime-async suspension bookkeeping to reference/cast the new ValueTaskSourceNotifier type.

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.

3 participants