Skip to content

Runtime async should avoid boxing custom awaiters on suspension #119842

Closed

Description

@jakobbotsch

Today for async1 the IL generated by Roslyn will save the awaiter into the display class as a normal field. The flow looks like:

  1. IsCompleted is checked
  2. If false, a copy of the awaiter value is copied into the display class
  3. AsyncTaskMethodBuilder.AwaitUnsafeOnCompleted is called on a local
  4. The call stack unwinds and continuations are queued. This requires synchronization since OnCompleted was already called, so the continuations may have already executed.

(As an aside, the fact that 2 and 3 do not happen on the same value is sort of strange, but presumably cannot be changed.)

For runtime async, however, the approach is different. It looks like:

  1. IsCompleted is checked
  2. If false immediately return. The call stack unwinds and continuations are created and linked. This does not require synchronization.
  3. OnCompleted is called with the head continuation (from the deepest call frame)

This approach means that we need to communicate the awaiter back from step 2 to step 3. That's currently being done by boxing it and saving it in TLS. We should see if we can find a way to avoid this boxing step. The value should already be in the continuation since GetResult is called after the await, so we can reuse it from there. Step 3 will then require some stub to extract and call OnCompleted on the awaiter (particularly because it needs to be done on a copy to match async1's semantics).

cc @VSadov @agocke @333fred

Activity

  1. added
    untriaged
    on Sep 18, 2025
  2. added
    area-CodeGen-coreclr
    on Sep 18, 2025
  3. added
    needs-area-label
    on Sep 18, 2025
  4. removed
    untriaged
    needs-area-label
    on Sep 18, 2025
  5. added this to the 11.0.0 milestone on Sep 18, 2025
  6. dotnet-policy-service commented on Sep 18, 2025

    @dotnet-policy-service
    Contributor

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

  7. jakobbotsch commented on Nov 6, 2025

    @jakobbotsch
    MemberAuthor

    We should keep this in mind as we work on #121013. The same mechanism we come up with should be general enough that we can emit lightweight OnComplete stubs too.

  8. added a commit that references this issue on Jul 3, 2026
    082f170
  9. added
    Priority:2
    on Jul 9, 2026
  10. added
    Priority:1
    and removed
    Priority:2
    on Jul 30, 2026
  11. locked and limited conversation to collaborators on Sep 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Priority:1area-CodeGen-coreclrruntime-async

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions