Skip to content

Consider supporting [AsyncMethodBuilder(typeof(PoolingAsyncValueTaskMethodBuilder))] in runtime-async #129004

Open

Description

@BrennanConroy

Right now methods marked with [AsyncMethodBuilder(typeof(PoolingAsyncValueTaskMethodBuilder))] are showing a regression from pre-runtime-async due to allocations. This issue is opened to consider if there should be support for AsyncMethodBuilder or a similar mechanism in runtime-async.

For now, runtime-async is being disabled for methods with [AsyncMethodBuilder(...)]. See #128943

@jakobbotsch

Activity

  1. added
    untriaged
    on Jun 4, 2026
  2. dotnet-policy-service commented on Jun 4, 2026

    @dotnet-policy-service
    Contributor

    Tagging subscribers to this area: @steveisok, @dotnet/area-system-reflection-emit
    See info in area-owners.md if you want to be subscribed.

  3. added
    area-CodeGen-coreclr
    and removed on Jun 5, 2026
  4. dotnet-policy-service commented on Jun 5, 2026

    @dotnet-policy-service
    Contributor

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

  5. JulieLeeMSFT commented on Jun 5, 2026

    @JulieLeeMSFT
    Member

    @jakobbotsch, putting this to future. Please feel free to pull in.

  6. removed
    untriaged
    on Jun 5, 2026
  7. added this to the Future milestone on Jun 5, 2026
  8. changed the title [-]Consider supporting `AsyncMethodBuilder` in runtime-async[/-] [+]Consider supporting `[AsyncMethodBuilder(typeof(PoolingAsyncValueTaskMethodBuilder))]` in runtime-async[/+] on Jun 8, 2026
  9. jakobbotsch commented on Jun 8, 2026

    @jakobbotsch
    Member

    I don't think we can support the completely general AsyncMethodBuilder, but we can introduce something like [PoolContinuations] that implements the same kind of poling as you would get using PoolingAsyncValueTaskMethodBuilder.

  10. timcassell commented on Jun 9, 2026

    @timcassell

    I don't think we can support the completely general AsyncMethodBuilder

    What about a new builder shape that the runtime can support?

  11. MichalPetryka commented on Jun 9, 2026

    @MichalPetryka
    Contributor

    I don't think we can support the completely general AsyncMethodBuilder, but we can introduce something like [PoolContinuations] that implements the same kind of poling as you would get using PoolingAsyncValueTaskMethodBuilder.

    Can't Async v2 check specifically for [AsyncMethodBuilder(typeof(PoolingAsyncValueTaskMethodBuilder))] and support only that one particular typeof there instead of handling the general case?

  12. VSadov commented on Jun 9, 2026

    @VSadov
    Member

    I don't think we can support the completely general AsyncMethodBuilder, but we can introduce something like [PoolContinuations] that implements the same kind of poling as you would get using PoolingAsyncValueTaskMethodBuilder.

    Can't Async v2 check specifically for [AsyncMethodBuilder(typeof(PoolingAsyncValueTaskMethodBuilder))] and support only that one particular typeof there instead of handling the general case?

    It was discussed as a possible option.

    The interesting part is implementing and hooking up caching of continuations. How the behavior is opted in for a given frame - there are multiple options. New attribute, existing attribute, ...

  13. VSadov commented on Jun 9, 2026

    @VSadov
    Member

    Also note dotnet/roslyn#84061

    It may be more consistent on the Roslyn side just opt out of runtime async anything with AsyncMethodBuilder. In general requiring a custom builder can easily be observable as we do not know what it does.

    From that point a new attribute could fit better as it would be free of old meanings.

  14. jkotas commented on Jun 9, 2026

    @jkotas
    Member

    What would it take to fix the runtime async infrastructure so that there is no benefit in manual control of continuation pooling?

    Manual opt-in into continuations pooling is a hard question. It helps in some cases and hurts in other cases. It would be best if people do not need to worry about it.

  15. MichalPetryka commented on Jun 9, 2026

    @MichalPetryka
    Contributor

    What would it take to fix the runtime async infrastructure so that there is no benefit in manual control of continuation pooling?

    Manual opt-in into continuations pooling is hard questions. It helps in some cases and hurts in other cases. It would be best if people do not need to worry about it.

    I agree, it'd be very nice if the runtime could automatically pool stuff and make it the least GC expensive version whenever possible.

  16. jakobbotsch commented on Jun 9, 2026

    @jakobbotsch
    Member

    What would it take to fix the runtime async infrastructure so that there is no benefit in manual control of continuation pooling?

    Manual opt-in into continuations pooling is a hard question. It helps in some cases and hurts in other cases. It would be best if people do not need to worry about it.

    We would probably need to look into some of these cases and understand how the pooling improves them. A priori it sounds a bit like designing something that outperforms the GC to me, so it seems like a hard task.

    I think in general the pooling is very rarely used. It seems to have been enabled on internal functions based on benchmarking, but in general I do not think people are worrying about it all that much.

    An interesting case (pointed at by @VSadov) is

    [AsyncMethodBuilder(typeof(PoolingAsyncValueTaskMethodBuilder))]
    public static async ValueTask InvokeAsync<TState>(Action<TState> action, TState state, CancellationToken cancellationToken)
    {
    // Queue the work to complete asynchronously. Logically, this is just queueing a work item to the thread pool.
    // We use a ForceYielding awaiter in combination with the PoolingAsyncValueTaskMethodBuilder to reduce allocation.
    await Task.CompletedTask.ConfigureAwait(ConfigureAwaitOptions.ForceYielding);
    // Register for cancellation, perform the work, and clean up. Even though we're in an async method, awaits _must not_ be used
    // after this point, or else the I/O cancellation could both not work and negatively interact with I/O on another thread.
    // The func _must_ be invoked on the same thread that invoked RegisterCancellation, with no intervening work.
    SyncAsyncWorkItemRegistration reg = RegisterCancellation(cancellationToken);
    try
    {
    action(state);
    }
    catch (OperationCanceledException oce) when (cancellationToken.IsCancellationRequested && oce.CancellationToken != cancellationToken)
    {
    throw CreateAppropriateCancellationException(cancellationToken, oce);
    }
    finally
    {
    reg.Dispose();
    }
    }

    If we had a silver bullet for runtime async then it seems the same technique would apply to make the thread pool cheaper in general. So this is probably at least as hard as improving allocation rates of the thread pool.

  17. 333fred commented on Jun 9, 2026

    @333fred
    Member

    Also note dotnet/roslyn#84061

    It may be more consistent on the Roslyn side just opt out of runtime async anything with AsyncMethodBuilder. In general requiring a custom builder can easily be observable as we do not know what it does.

    From that point a new attribute could fit better as it would be free of old meanings.

    Highly agreed. A world in which he AsyncMethodBuilder attribute silently works sometimes is a recipe for confusion. It needs to be a binary "this always falls back", or it needs to be an error that requires the user to explicitly opt out.

  18. VSadov commented on Jun 9, 2026

    @VSadov
    Member

    Manual opt-in into continuations pooling is a hard question. It helps in some cases and hurts in other cases. It would be best if people do not need to worry about it.

    The scenario where pooling is used is when you have a frequently suspending frame, but it is not an "await in a loop" case, thus you have no benefits from the regular continuation reuse.
    In such case you could use a more long-lived cache. To not become a memory leak the cache must have a limit on its size. It is also per type of the continuation/statemachine, which adds to complexity.

    It is indeed an attempt to be better than GC at short-term allocations. My guess is that it is profitable only in very specific cases and thus very rarely used.
    Note that you need to clear the continuation between reuses, and GC is fairly efficient at clearing memory. I think the primary targeted hazard is eventual GC pauses triggered by burning through Gen0 budget and pooling may delay that, if otherwise Gen0 churn is low.

    So far this kind of caching was not a high priority, but I am afraid we will eventually face the same reasons that forced async1 to implement this.

    What would it take to fix the runtime async infrastructure so that there is no benefit in manual control of continuation pooling?

    I am not sure there is a solution that helps all cases here, other than improving GC.
    Pooling everything will likely result in a cache that is huge and unprofitable.

  19. jakobbotsch commented on Jun 10, 2026

    @jakobbotsch
    Member

    I don't think we can support the completely general AsyncMethodBuilder

    What about a new builder shape that the runtime can support?

    I think we would need a strong motivating scenario to investigate building something like that and to understand what kind of behavior wants to be modified. The motivating scenarios for async1 were value tasks and pooling, and the builder support allowed overriding existing APIs already called by Roslyn, but for runtime async there are much fewer hooking points.

  20. timcassell commented on Jun 10, 2026

    @timcassell

    I think we would need a strong motivating scenario to investigate building something like that and to understand what kind of behavior wants to be modified.

    I'd want it to bring the extra performance to existing async libraries like UniTask and ProtoPromise. Then users would get the extra perf just by upgrading without having to switch all their code to BCL tasks, and since it's runtime-controlled, the perf would span across different async types without them needing internal knowledge of each other.

  21. jakobbotsch commented on Jun 10, 2026

    @jakobbotsch
    Member

    Runtime async achieves improvements by making many assumptions about how Task and ValueTask work. For one, runtime async does not even construct instances of Task or ValueTask in most cases, so what should it do when the return type is some custom task-like type? There are many questions like that to answer.

  22. timcassell commented on Jun 10, 2026

    @timcassell

    I would think that if a type opts into the new runtime impl, it assumes the semantics are identical to tasks. Not actually creating the type instance when it's functionally not needed is a good thing. The main differences between those 2 libs and tasks are object pooling, not capturing the execution context, and synchronous continuations by default; all performance reasons. We're already talking about object pooling for ValueTask here, and AFAIK runtime-async optimizes away the execution context when it's not used (at least that was the plan when I read about it a while ago), so the only thing that leaves is the continuation behavior. Runtime-async already supports ConfigureAwait, so the question becomes how to support that generally.

  23. JulieLeeMSFT commented on Jun 10, 2026

    @JulieLeeMSFT
    Member

    I am not sure there is a solution that helps all cases here, other than improving GC.

    CC @anicka-net, @janvorli.

  24. added
    design-discussion
    on Jun 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area-CodeGen-coreclrdesign-discussionruntime-async

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions