Skip to content

Avoid duplicate ValueTask checks in transparent awaits - #133023

Open
jakobbotsch wants to merge 1 commit into
dotnet:mainfrom
jakobbotsch:runtime-async-transparent-await
Open

Avoid duplicate ValueTask checks in transparent awaits#133023
jakobbotsch wants to merge 1 commit into
dotnet:mainfrom
jakobbotsch:runtime-async-transparent-await

Conversation

@jakobbotsch

@jakobbotsch jakobbotsch commented Sep 1, 2026

Copy link
Copy Markdown
Member

Classify ValueTask backing objects once in TransparentAwait and pass typed Task or IValueTaskSource instances to the suspend helpers. This mirrors the existing AsyncHelpers.Await implementation and removes repeated type checks and temporary ValueTask materialization from runtime-async tail awaits.

This is about 2% RPS improvement on the TechEmpower Arm64 JSON benchmarks. That benchmark was spending a not insignificant time in CastHelpers.IsInstanceOfClass invoked by the duplicated is Task<T> check that was inside AsyncHelpers.TransparentSuspend. The primary source of that comes from SocketReceiver.WaitForDataAsync.

Co-authored-by: Copilot 223556219+Copilot@users.noreply.github.com

Classify ValueTask backing objects once in TransparentAwait and pass
typed Task or IValueTaskSource instances to the suspend helpers. This
mirrors the existing AsyncHelpers.Await implementation and removes
repeated type checks and temporary ValueTask materialization from
runtime-async tail awaits.

This is about 2% RPS improvement on the TechEmpower Arm64 JSON
benchmarks. That benchmark was spending a not insignificant time in
`CastHelpers.IsInstanceOfType` invoked by the duplicated `is Task<T>`
check that was inside AsyncHelpers.TransparentSuspend.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot-Session: 1d56f651-7596-4378-81ee-21221f3c746e
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 3 pipeline(s).
13 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

@jakobbotsch
jakobbotsch force-pushed the runtime-async-transparent-await branch from 6631be7 to 0d9e098 Compare September 1, 2026 14:49

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🔵 Needs a closer look

Pull request overview

This PR refactors the CoreCLR AsyncHelpers.TransparentAwait path for ValueTask/ValueTask<T> to classify the backing object once (Task vs IValueTaskSource*) and then call the appropriate suspend helper with the already-typed backing object, avoiding repeated runtime type checks during transparent/tail awaits.

Changes:

  • Changes TransparentSuspend(ValueTask) / TransparentSuspend(ValueTask<T>) into TransparentSuspend(IValueTaskSource, short) / TransparentSuspend(IValueTaskSource<T>, short) and keeps Task-backed transparent suspension on the existing TransparentSuspend(Task) / TransparentSuspend(Task<T>) helpers.
  • Updates TransparentAwait(ValueTask) / TransparentAwait(ValueTask<T>) to directly branch on task._obj and invoke TaskAwaiter.ValidateEnd or IValueTaskSource*.GetResult as appropriate without re-materializing/recursively re-checking the ValueTask.
File summaries
File Description
src/coreclr/System.Private.CoreLib/src/System/Runtime/CompilerServices/AsyncHelpers.CoreCLR.cs Moves ValueTask backing-object classification into TransparentAwait and passes typed backing objects to suspend helpers to reduce duplicated checks on the hot transparent-await path.
Review details
  • Files reviewed: 1/1 changed files
  • Comments generated: 0
  • Review effort level: Lite

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @dotnet/area-system-threading-tasks
See info in area-owners.md if you want to be subscribed.

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