iTranslated by AI
First Look at Runtime Async in .NET 11
.NET 11.0 Preview 1[1] has been released. I'm going to take a look at Runtime Async.
Wait, it's gotten slower!
Whoa, I thought it was going to be super fast, but it turned out to be going from nanoseconds to microseconds. I guess it's still in the "we just made it work for now" stage.
I have a hunch that "Runtime Async isn't about speed. If you want speed, you should build with Native AOT?" might become the prevailing sentiment.
.NET 10.0.3
| Method | Mean | Error | StdDev | Gen0 | Allocated |
|---|---|---|---|---|---|
| RuntimeAsync | 804.9 ns | 4.86 ns | 4.54 ns | 0.0267 | 224 B |
.NET 11.0-preview.1
| Method | Mean | Error | StdDev | Gen0 | Allocated |
|---|---|---|---|---|---|
| RuntimeAsync | 1.126 us | 0.0070 us | 0.0062 us | 0.0439 | 376 B |
What is Runtime Async?
To begin with, asynchronous processing involves quite a few things.
👇 Podcast version
Until Now
Up until .NET 10.0, the "results of complex operations" were included in the assembly.
In other words, if you had async methods FooAsync and BarAsync,
- FooAsync
- FooAsync_StateMachine
- BarAsync
- BarAsync_StateMachie
The async methods and their corresponding types were included in the assembly like that. This was done for every single async method in the standard library. That sounds like it would make the build size massive!
From Now On (Runtime Async)
When you build with Runtime Async enabled, async methods are built as "async methods." In other words:
- FooAsync
FooAsync_StateMachine- BarAsync
BarAsync_StateMachie
Since the state machines that were created for every method are removed from the assembly, the build size will become smaller (presumably), and the build itself will (presumably) become faster.
On the other hand, as seen in the benchmarks earlier, it's only natural that performance would worsen. After all, it's generating state machines at runtime. Is performance optimization fundamentally impossible? It would be a success if optimization brought it back to the same speed as .NET 10.0, and in the future, we might see hacks like disabling Runtime Async.
As a matter of principle, asynchronous processing is a technology for using resources efficiently in IO-bound environments, and it sacrifices processing speed to some extent for that purpose. The answer seems to be "Build with Native AOT" or "Use Task.Run to change from concurrent to parallel processing."
IL Code Comparison
As mentioned above, enabling Runtime Async results in async methods being output as async methods, so the IL code becomes much shorter.
It seems there are about 1,600 files[2] containing async in the BCL, and all of these state machines will be removed from the build.
As a result, we can expect benefits that don't show up as numbers in benchmarks, such as faster load times due to a reduced number of types and improvements in memory footprint.
Source code used for testing
namespace RIDER_TEST;
public class Simple
{
private static async ValueTask FooAsync(int[] values)
{
await Task.CompletedTask.ConfigureAwait(ConfigureAwaitOptions.ForceYielding);
await EmptyAsync();
}
private static ValueTask EmptyAsync()
{
return default;
}
}
.NET 10.0
It's long! (The state machine code is baked in directly)
.class public auto ansi beforefieldinit
RIDER_TEST.Simple
extends [System.Runtime]System.Object
{
.class nested private sealed auto ansi beforefieldinit
'<FooAsync>d__0'
extends [System.Runtime]System.ValueType
implements [System.Runtime]System.Runtime.CompilerServices.IAsyncStateMachine
{
.custom instance void [System.Runtime]System.Runtime.CompilerServices.CompilerGeneratedAttribute::.ctor()
= (01 00 00 00 )
.field public int32 '<>1__state'
.field public valuetype [System.Runtime]System.Runtime.CompilerServices.AsyncValueTaskMethodBuilder '<>t__builder'
.field private valuetype [System.Runtime]System.Runtime.CompilerServices.ConfiguredTaskAwaitable/ConfiguredTaskAwaiter '<>u__1'
.field private valuetype [System.Runtime]System.Runtime.CompilerServices.ValueTaskAwaiter '<>u__2'
.method private final hidebysig virtual newslot instance void
MoveNext() cil managed
{
.override method instance void [System.Runtime]System.Runtime.CompilerServices.IAsyncStateMachine::MoveNext()
.maxstack 3
.locals init (
[0] int32 V_0,
[1] valuetype [System.Runtime]System.Runtime.CompilerServices.ConfiguredTaskAwaitable/ConfiguredTaskAwaiter V_1,
[2] valuetype [System.Runtime]System.Runtime.CompilerServices.ConfiguredTaskAwaitable V_2,
[3] valuetype [System.Runtime]System.Runtime.CompilerServices.ValueTaskAwaiter V_3,
[4] valuetype [System.Runtime]System.Threading.Tasks.ValueTask V_4,
[5] class [System.Runtime]System.Exception V_5
)
IL_0000: ldarg.0 // this
IL_0001: ldfld int32 RIDER_TEST.Simple/'<FooAsync>d__0'::'<>1__state'
IL_0006: stloc.0 // V_0
.try
{
IL_0007: ldloc.0 // V_0
IL_0008: brfalse.s IL_0051
IL_000a: ldloc.0 // V_0
IL_000b: ldc.i4.1
IL_000c: beq IL_00ac
// [7 9 - 7 86]
IL_0011: call class [System.Runtime]System.Threading.Tasks.Task [System.Runtime]System.Threading.Tasks.Task::get_CompletedTask()
IL_0016: ldc.i4.4
IL_0017: callvirt instance valuetype [System.Runtime]System.Runtime.CompilerServices.ConfiguredTaskAwaitable [System.Runtime]System.Threading.Tasks.Task::ConfigureAwait(valuetype [System.Runtime]System.Threading.Tasks.ConfigureAwaitOptions)
IL_001c: stloc.2 // V_2
IL_001d: ldloca.s V_2
IL_001f: call instance valuetype [System.Runtime]System.Runtime.CompilerServices.ConfiguredTaskAwaitable/ConfiguredTaskAwaiter [System.Runtime]System.Runtime.CompilerServices.ConfiguredTaskAwaitable::GetAwaiter()
IL_0024: stloc.1 // V_1
IL_0025: ldloca.s V_1
IL_0027: call instance bool [System.Runtime]System.Runtime.CompilerServices.ConfiguredTaskAwaitable/ConfiguredTaskAwaiter::get_IsCompleted()
IL_002c: brtrue.s IL_006d
IL_002e: ldarg.0 // this
IL_002f: ldc.i4.0
IL_0030: dup
IL_0031: stloc.0 // V_0
IL_0032: stfld int32 RIDER_TEST.Simple/'<FooAsync>d__0'::'<>1__state'
IL_0037: ldarg.0 // this
IL_0038: ldloc.1 // V_1
IL_0039: stfld valuetype [System.Runtime]System.Runtime.CompilerServices.ConfiguredTaskAwaitable/ConfiguredTaskAwaiter RIDER_TEST.Simple/'<FooAsync>d__0'::'<>u__1'
IL_003e: ldarg.0 // this
IL_003f: ldflda valuetype [System.Runtime]System.Runtime.CompilerServices.AsyncValueTaskMethodBuilder RIDER_TEST.Simple/'<FooAsync>d__0'::'<>t__builder'
IL_0044: ldloca.s V_1
IL_0046: ldarg.0 // this
IL_0047: call instance void [System.Runtime]System.Runtime.CompilerServices.AsyncValueTaskMethodBuilder::AwaitUnsafeOnCompleted<valuetype [System.Runtime]System.Runtime.CompilerServices.ConfiguredTaskAwaitable/ConfiguredTaskAwaiter, valuetype RIDER_TEST.Simple/'<FooAsync>d__0'>(!!0/*valuetype [System.Runtime]System.Runtime.CompilerServices.ConfiguredTaskAwaitable/ConfiguredTaskAwaiter*/&, !!1/*valuetype RIDER_TEST.Simple/'<FooAsync>d__0'*/&)
IL_004c: leave IL_00fd
IL_0051: ldarg.0 // this
IL_0052: ldfld valuetype [System.Runtime]System.Runtime.CompilerServices.ConfiguredTaskAwaitable/ConfiguredTaskAwaiter RIDER_TEST.Simple/'<FooAsync>d__0'::'<>u__1'
IL_0057: stloc.1 // V_1
IL_0058: ldarg.0 // this
IL_0059: ldflda valuetype [System.Runtime]System.Runtime.CompilerServices.ConfiguredTaskAwaitable/ConfiguredTaskAwaiter RIDER_TEST.Simple/'<FooAsync>d__0'::'<>u__1'
IL_005e: initobj [System.Runtime]System.Runtime.CompilerServices.ConfiguredTaskAwaitable/ConfiguredTaskAwaiter
IL_0064: ldarg.0 // this
IL_0065: ldc.i4.m1
IL_0066: dup
IL_0067: stloc.0 // V_0
IL_0068: stfld int32 RIDER_TEST.Simple/'<FooAsync>d__0'::'<>1__state'
IL_006d: ldloca.s V_1
IL_006f: call instance void [System.Runtime]System.Runtime.CompilerServices.ConfiguredTaskAwaitable/ConfiguredTaskAwaiter::GetResult()
// [8 9 - 8 28]
IL_0074: call valuetype [System.Runtime]System.Threading.Tasks.ValueTask RIDER_TEST.Simple::EmptyAsync()
IL_0079: stloc.s V_4
IL_007b: ldloca.s V_4
IL_007d: call instance valuetype [System.Runtime]System.Runtime.CompilerServices.ValueTaskAwaiter [System.Runtime]System.Threading.Tasks.ValueTask::GetAwaiter()
IL_0082: stloc.3 // V_3
IL_0083: ldloca.s V_3
IL_0085: call instance bool [System.Runtime]System.Runtime.CompilerServices.ValueTaskAwaiter::get_IsCompleted()
IL_008a: brtrue.s IL_00c8
IL_008c: ldarg.0 // this
IL_008d: ldc.i4.1
IL_008e: dup
IL_008f: stloc.0 // V_0
IL_0090: stfld int32 RIDER_TEST.Simple/'<FooAsync>d__0'::'<>1__state'
IL_0095: ldarg.0 // this
IL_0096: ldloc.3 // V_3
IL_0097: stfld valuetype [System.Runtime]System.Runtime.CompilerServices.ValueTaskAwaiter RIDER_TEST.Simple/'<FooAsync>d__0'::'<>u__2'
IL_009c: ldarg.0 // this
IL_009d: ldflda valuetype [System.Runtime]System.Runtime.CompilerServices.AsyncValueTaskMethodBuilder RIDER_TEST.Simple/'<FooAsync>d__0'::'<>t__builder'
IL_00a2: ldloca.s V_3
IL_00a4: ldarg.0 // this
IL_00a5: call instance void [System.Runtime]System.Runtime.CompilerServices.AsyncValueTaskMethodBuilder::AwaitUnsafeOnCompleted<valuetype [System.Runtime]System.Runtime.CompilerServices.ValueTaskAwaiter, valuetype RIDER_TEST.Simple/'<FooAsync>d__0'>(!!0/*valuetype [System.Runtime]System.Runtime.CompilerServices.ValueTaskAwaiter*/&, !!1/*valuetype RIDER_TEST.Simple/'<FooAsync>d__0'*/&)
IL_00aa: leave.s IL_00fd
IL_00ac: ldarg.0 // this
IL_00ad: ldfld valuetype [System.Runtime]System.Runtime.CompilerServices.ValueTaskAwaiter RIDER_TEST.Simple/'<FooAsync>d__0'::'<>u__2'
IL_00b2: stloc.3 // V_3
IL_00b3: ldarg.0 // this
IL_00b4: ldflda valuetype [System.Runtime]System.Runtime.CompilerServices.ValueTaskAwaiter RIDER_TEST.Simple/'<FooAsync>d__0'::'<>u__2'
IL_00b9: initobj [System.Runtime]System.Runtime.CompilerServices.ValueTaskAwaiter
IL_00bf: ldarg.0 // this
IL_00c0: ldc.i4.m1
IL_00c1: dup
IL_00c2: stloc.0 // V_0
IL_00c3: stfld int32 RIDER_TEST.Simple/'<FooAsync>d__0'::'<>1__state'
IL_00c8: ldloca.s V_3
IL_00ca: call instance void [System.Runtime]System.Runtime.CompilerServices.ValueTaskAwaiter::GetResult()
IL_00cf: leave.s IL_00ea
} // end of .try
catch [System.Runtime]System.Exception
{
IL_00d1: stloc.s V_5
IL_00d3: ldarg.0 // this
IL_00d4: ldc.i4.s -2 // 0xfe
IL_00d6: stfld int32 RIDER_TEST.Simple/'<FooAsync>d__0'::'<>1__state'
IL_00db: ldarg.0 // this
IL_00dc: ldflda valuetype [System.Runtime]System.Runtime.CompilerServices.AsyncValueTaskMethodBuilder RIDER_TEST.Simple/'<FooAsync>d__0'::'<>t__builder'
IL_00e1: ldloc.s V_5
IL_00e3: call instance void [System.Runtime]System.Runtime.CompilerServices.AsyncValueTaskMethodBuilder::SetException(class [System.Runtime]System.Exception)
IL_00e8: leave.s IL_00fd
} // end of catch
// [9 5 - 9 6]
IL_00ea: ldarg.0 // this
IL_00eb: ldc.i4.s -2 // 0xfe
IL_00ed: stfld int32 RIDER_TEST.Simple/'<FooAsync>d__0'::'<>1__state'
IL_00f2: ldarg.0 // this
IL_00f3: ldflda valuetype [System.Runtime]System.Runtime.CompilerServices.AsyncValueTaskMethodBuilder RIDER_TEST.Simple/'<FooAsync>d__0'::'<>t__builder'
IL_00f8: call instance void [System.Runtime]System.Runtime.CompilerServices.AsyncValueTaskMethodBuilder::SetResult()
IL_00fd: ret
} // end of method '<FooAsync>d__0'::MoveNext
.method private final hidebysig virtual newslot instance void
SetStateMachine(
class [System.Runtime]System.Runtime.CompilerServices.IAsyncStateMachine stateMachine
) cil managed
{
.custom instance void [System.Runtime]System.Diagnostics.DebuggerHiddenAttribute::.ctor()
= (01 00 00 00 )
.param [1]
.custom instance void [System.Runtime]System.Runtime.CompilerServices.NullableAttribute::.ctor(unsigned int8)
= (01 00 01 00 00 ) // .....
// unsigned int8(1) // 0x01
.override method instance void [System.Runtime]System.Runtime.CompilerServices.IAsyncStateMachine::SetStateMachine(class [System.Runtime]System.Runtime.CompilerServices.IAsyncStateMachine)
.maxstack 8
IL_0000: ldarg.0 // this
IL_0001: ldflda valuetype [System.Runtime]System.Runtime.CompilerServices.AsyncValueTaskMethodBuilder RIDER_TEST.Simple/'<FooAsync>d__0'::'<>t__builder'
IL_0006: ldarg.1 // stateMachine
IL_0007: call instance void [System.Runtime]System.Runtime.CompilerServices.AsyncValueTaskMethodBuilder::SetStateMachine(class [System.Runtime]System.Runtime.CompilerServices.IAsyncStateMachine)
IL_000c: ret
} // end of method '<FooAsync>d__0'::SetStateMachine
} // end of class '<FooAsync>d__0'
.method private hidebysig static valuetype [System.Runtime]System.Threading.Tasks.ValueTask
FooAsync(
int32[] values
) cil managed
{
.custom instance void [System.Runtime]System.Runtime.CompilerServices.NullableContextAttribute::.ctor(unsigned int8)
= (01 00 01 00 00 ) // .....
// unsigned int8(1) // 0x01
.custom instance void [System.Runtime]System.Runtime.CompilerServices.AsyncStateMachineAttribute::.ctor(class [System.Runtime]System.Type)
= (
01 00 20 52 49 44 45 52 5f 54 45 53 54 2e 53 69 // .. RIDER_TEST.Si
6d 70 6c 65 2b 3c 46 6f 6f 41 73 79 6e 63 3e 64 // mple+<FooAsync>d
5f 5f 30 00 00 // __0..
)
// type(valuetype RIDER_TEST.Simple/'<FooAsync>d__0')
.maxstack 2
.locals init (
[0] valuetype RIDER_TEST.Simple/'<FooAsync>d__0' V_0
)
IL_0000: ldloca.s V_0
IL_0002: call valuetype [System.Runtime]System.Runtime.CompilerServices.AsyncValueTaskMethodBuilder [System.Runtime]System.Runtime.CompilerServices.AsyncValueTaskMethodBuilder::Create()
IL_0007: stfld valuetype [System.Runtime]System.Runtime.CompilerServices.AsyncValueTaskMethodBuilder RIDER_TEST.Simple/'<FooAsync>d__0'::'<>t__builder'
IL_000c: ldloca.s V_0
IL_000e: ldc.i4.m1
IL_000f: stfld int32 RIDER_TEST.Simple/'<FooAsync>d__0'::'<>1__state'
IL_0014: ldloca.s V_0
IL_0016: ldflda valuetype [System.Runtime]System.Runtime.CompilerServices.AsyncValueTaskMethodBuilder RIDER_TEST.Simple/'<FooAsync>d__0'::'<>t__builder'
IL_001b: ldloca.s V_0
IL_001d: call instance void [System.Runtime]System.Runtime.CompilerServices.AsyncValueTaskMethodBuilder::Start<valuetype RIDER_TEST.Simple/'<FooAsync>d__0'>(!!0/*valuetype RIDER_TEST.Simple/'<FooAsync>d__0'*/&)
IL_0022: ldloca.s V_0
IL_0024: ldflda valuetype [System.Runtime]System.Runtime.CompilerServices.AsyncValueTaskMethodBuilder RIDER_TEST.Simple/'<FooAsync>d__0'::'<>t__builder'
IL_0029: call instance valuetype [System.Runtime]System.Threading.Tasks.ValueTask [System.Runtime]System.Runtime.CompilerServices.AsyncValueTaskMethodBuilder::get_Task()
IL_002e: ret
} // end of method Simple::FooAsync
.method private hidebysig static valuetype [System.Runtime]System.Threading.Tasks.ValueTask
EmptyAsync() cil managed
{
.maxstack 1
.locals init (
[0] valuetype [System.Runtime]System.Threading.Tasks.ValueTask V_0
)
// [13 9 - 13 24]
IL_0000: ldloca.s V_0
IL_0002: initobj [System.Runtime]System.Threading.Tasks.ValueTask
IL_0008: ldloc.0 // V_0
IL_0009: ret
} // end of method Simple::EmptyAsync
.method public hidebysig specialname rtspecialname instance void
.ctor() cil managed
{
.maxstack 8
IL_0000: ldarg.0 // this
IL_0001: call instance void [System.Runtime]System.Object::.ctor()
IL_0006: ret
} // end of method Simple::.ctor
} // end of class RIDER_TEST.Simple
.NET 11.0 Preview 1
If .NET 10 is const, think of .NET 11 as static readonly. Since it's not baked into the assembly, there is room for speed improvements just by changing the execution environment without recompiling.
.class public auto ansi beforefieldinit
RIDER_TEST.Simple
extends [System.Runtime]System.Object
{
.method private hidebysig static valuetype [System.Runtime]System.Threading.Tasks.ValueTask
FooAsync(
int32[] values
) cil managed
{
.custom instance void [System.Runtime]System.Runtime.CompilerServices.NullableContextAttribute::.ctor(unsigned int8)
= (01 00 01 00 00 ) // .....
// unsigned int8(1) // 0x01
.maxstack 8
// [7 9 - 7 86]
IL_0000: call class [System.Runtime]System.Threading.Tasks.Task [System.Runtime]System.Threading.Tasks.Task::get_CompletedTask()
IL_0005: ldc.i4.4
IL_0006: callvirt instance valuetype [System.Runtime]System.Runtime.CompilerServices.ConfiguredTaskAwaitable [System.Runtime]System.Threading.Tasks.Task::ConfigureAwait(valuetype [System.Runtime]System.Threading.Tasks.ConfigureAwaitOptions)
IL_000b: call void [System.Runtime]System.CompilerServices.AsyncHelpers::Await(valuetype [System.Runtime]System.Runtime.CompilerServices.ConfiguredTaskAwaitable)
// [8 9 - 8 28]
IL_0010: call valuetype [System.Runtime]System.Threading.Tasks.ValueTask RIDER_TEST.Simple::EmptyAsync()
IL_0015: call void [System.Runtime]System.CompilerServices.AsyncHelpers::Await(valuetype [System.Runtime]System.Threading.Tasks.ValueTask)
IL_001a: ret
} // end of method Simple::FooAsync
.method private hidebysig static valuetype [System.Runtime]System.Threading.Tasks.ValueTask
EmptyAsync() cil managed
{
.maxstack 1
.locals init (
[0] valuetype [System.Runtime]System.Threading.Tasks.ValueTask V_0
)
// [13 9 - 13 24]
IL_0000: ldloca.s V_0
IL_0002: initobj [System.Runtime]System.Threading.Tasks.ValueTask
IL_0008: ldloc.0 // V_0
IL_0009: ret
} // end of method Simple::EmptyAsync
.method public hidebysig specialname rtspecialname instance void
.ctor() cil managed
{
.maxstack 8
IL_0000: ldarg.0 // this
IL_0001: call instance void [System.Runtime]System.Object::.ctor()
IL_0006: ret
} // end of method Simple::.ctor
} // end of class RIDER_TEST.Simple
Full Test Project
I was hoping for 👇, so I put new int[] { 1, 2, 3 } in, but it didn't have much of an effect.
It wasn't inlined and turned into stackalloc, and it doesn't seem like the empty EmptyAsync disappears at runtime (yet)?
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFrameworks>net11.0;net10.0</TargetFrameworks>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<PropertyGroup Condition=" '$(TargetFramework)' == 'net11.0' ">
<EnablePreviewFeatures>true</EnablePreviewFeatures>
<Features>$(Features);runtime-async=on</Features>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.15.8" />
</ItemGroup>
</Project>
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Configs;
using BenchmarkDotNet.Jobs;
using BenchmarkDotNet.Running;
using BenchmarkDotNet.Toolchains.InProcess.Emit;
namespace RIDER_TEST;
[MemoryDiagnoser]
public class Program
{
public static void Main(string[] args)
{
var job = Job.Default
.WithToolchain(InProcessEmitToolchain.Instance);
var config = DefaultConfig.Instance
.AddJob(job)
.WithOptions(ConfigOptions.DisableOptimizationsValidator);
BenchmarkRunner.Run<Program>(config);
}
[GlobalSetup]
public void BenchmarkSetup()
{
// optional setup code...
}
[Benchmark]
public async ValueTask RuntimeAsync()
{
await FooAsync(new int[] { 1, 2, 3 });
}
private static async ValueTask FooAsync(int[] values)
{
await Task.CompletedTask.ConfigureAwait(ConfigureAwaitOptions.ForceYielding);
await EmptyAsync();
}
private static ValueTask EmptyAsync()
{
return default;
}
}
Closing Thoughts
I wonder what will happen with Unity's IL2CPP? Simply converting it to C++ is no longer enough.
That's all for now. Thanks for reading.
References
-
.NET 11 Preview 1 is now available! — https://devblogs.microsoft.com/dotnet/dotnet-11-preview-1/ ↩︎
-
repo:dotnet/runtime ("async Task" OR "async ValueTask") language:C#— https://github.com/search?q=repo%3Adotnet%2Fruntime+("async+Task"+OR+"async+ValueTask")+language%3AC%23&type=code ↩︎
Discussion