You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Consider supporting [AsyncMethodBuilder(typeof(PoolingAsyncValueTaskMethodBuilder))] in runtime-async #129004
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
changed the title [-]Consider supporting `AsyncMethodBuilder` in runtime-async[/-][+]Consider supporting `[AsyncMethodBuilder(typeof(PoolingAsyncValueTaskMethodBuilder))]` in runtime-async[/+]on Jun 8, 2026
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.
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?
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, ...
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
Activity
dotnet-policy-service commentedon Jun 4, 2026
Tagging subscribers to this area: @steveisok, @dotnet/area-system-reflection-emit
See info in area-owners.md if you want to be subscribed.
dotnet-policy-service commentedon Jun 5, 2026
Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch
See info in area-owners.md if you want to be subscribed.
JulieLeeMSFT commentedon Jun 5, 2026
@jakobbotsch, putting this to future. Please feel free to pull in.
[-]Consider supporting `AsyncMethodBuilder` in runtime-async[/-][+]Consider supporting `[AsyncMethodBuilder(typeof(PoolingAsyncValueTaskMethodBuilder))]` in runtime-async[/+]jakobbotsch commentedon Jun 8, 2026
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 usingPoolingAsyncValueTaskMethodBuilder.timcassell commentedon Jun 9, 2026
What about a new builder shape that the runtime can support?
MichalPetryka commentedon Jun 9, 2026
Can't Async v2 check specifically for
[AsyncMethodBuilder(typeof(PoolingAsyncValueTaskMethodBuilder))]and support only that one particulartypeofthere instead of handling the general case?VSadov commentedon Jun 9, 2026
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, ...
VSadov commentedon Jun 9, 2026
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.
jkotas commentedon Jun 9, 2026
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.
MichalPetryka commentedon Jun 9, 2026
I agree, it'd be very nice if the runtime could automatically pool stuff and make it the least GC expensive version whenever possible.
jakobbotsch commentedon Jun 9, 2026
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
runtime/src/libraries/Common/src/System/Threading/AsyncOverSyncWithIoCancellation.cs
Lines 84 to 107 in bba72ce
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.
333fred commentedon Jun 9, 2026
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.
VSadov commentedon Jun 9, 2026
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.
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.
jakobbotsch commentedon Jun 10, 2026
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.
timcassell commentedon Jun 10, 2026
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.
jakobbotsch commentedon Jun 10, 2026
Runtime async achieves improvements by making many assumptions about how
TaskandValueTaskwork. For one, runtime async does not even construct instances ofTaskorValueTaskin 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.timcassell commentedon Jun 10, 2026
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.
JulieLeeMSFT commentedon Jun 10, 2026
CC @anicka-net, @janvorli.