async/await in .NET: How await Got Cheaper Since 2012
In the first part of this series we followed .NET concurrency from Thread to the Task Parallel Library and ended on a question: what if the compiler wrote the continuations for us? That question was answered in 2012, and I still consider the answer one of the best language design decisions in the history of C#. This part follows it through every release since, from the Async CTP to .NET 10, with Microsoft’s published numbers and one benchmark suite of my own.
2012: The Compiler Takes Over the Continuations
Anders Hejlsberg announced the Visual Studio Async CTP (Community Technology Preview) at the Professional Developers Conference (PDC) in October 2010. Stephen Toub described its goal as making asynchronous code “almost as straightforward as synchronous code”, expressed entirely in terms of Task. The feature shipped as C# 5 with .NET Framework 4.5 and Visual Studio 2012, together with “hundreds of new Task-returning methods” across the framework libraries.
Remember the little chain from Part 1 that turned 21 into “The answer is 42” with a StartNew and two ContinueWith lambdas? Written the 2012 way, it shrinks to this:
// .NET Framework 4.5 and later: Part 1's chain, written with await.
static async Task<string> AnswerAsync()
{
int value = await Task.Run(() => 21);
return "The answer is " + value * 2;
}
Same Task, same thread pool, same answer. Task.Run is the 4.5 shortcut for Task.Factory.StartNew, and the continuation is simply the rest of the method. Loops, try/catch and using keep working across an await as if it were an ordinary call.
It was a relay, not an invention from nothing. Don Syme called the C# and VB design “heavily inspired by the corresponding feature of F#”, and it rested on the .NET 4 tasks. Toub’s How Async/Await Really Works adds that roughly 95% of the compiler logic for iterators and for async is shared, and that the experimental Axum language prototyped a similar transformation. Eric Lippert’s framing is the shortest version. The designers of C# 2.0 realized that writing iterator logic was painful, so they added iterator blocks, and the designers of C# 5.0 realized that writing asynchronous code was painful, so they added async.
The best part of the 2012 design is what it did not need. It is a compiler feature plus a handful of library types, and the runtime did not have to learn anything new. The async keyword is not even part of the method signature, as Toub points out. Like unsafe, it is an implementation detail. That is why Microsoft.Bcl.Async could bring the same keywords to .NET 4, Silverlight 4 and Windows Phone 7.5. Few language features have shipped with such a small blast radius and such a large effect on how we write code.
What the compiler writes
A Release build turns AnswerAsync into roughly the following, simplified by hand. I compiled both for .NET Framework 4.8 and for .NET 11, and both print “The answer is 42”:
// Roughly what the compiler writes for AnswerAsync (hand-simplified).
static Task<string> AnswerAsyncLowered()
{
var machine = new AnswerStateMachine
{
Builder = AsyncTaskMethodBuilder<string>.Create(),
State = -1,
};
machine.Builder.Start(ref machine);
return machine.Builder.Task;
}
struct AnswerStateMachine : IAsyncStateMachine
{
public int State;
public AsyncTaskMethodBuilder<string> Builder;
private TaskAwaiter<int> _awaiter;
public void MoveNext()
{
string result;
try
{
TaskAwaiter<int> awaiter;
if (State == 0)
{
awaiter = _awaiter; // resumed: pick the awaiter back up
_awaiter = default;
State = -1;
}
else
{
awaiter = Task.Run(() => 21).GetAwaiter();
if (!awaiter.IsCompleted)
{
State = 0; // remember where we are
_awaiter = awaiter;
Builder.AwaitUnsafeOnCompleted(ref awaiter, ref this);
return; // suspend: the caller gets the Task
}
}
int value = awaiter.GetResult();
result = "The answer is " + value * 2;
}
catch (Exception ex)
{
State = -2;
Builder.SetException(ex);
return;
}
State = -2;
Builder.SetResult(result);
}
public void SetStateMachine(IAsyncStateMachine stateMachine) => Builder.SetStateMachine(stateMachine);
}
Three details carry most of the design. First, the state machine is a struct that starts life on the stack. If the awaited operation is already complete, IsCompleted is true, the method runs to the end in one MoveNext call and, as Toub writes, the machine never leaves the stack. Second, the builder owns the returned task: SetResult and SetException complete it, and AwaitUnsafeOnCompleted hooks up the continuation. A cancellation exception ends the task as canceled, anything else as faulted. Third, await binds to a pattern (GetAwaiter, and an awaiter with IsCompleted, GetResult and INotifyCompletion.OnCompleted), not to Task, which is why ValueTask, YieldAwaitable and your own awaiters all work. One practical note: Debug builds generate the machine as a class to help debugging, so never benchmark async code in Debug.
The most consequential decision is easy to overlook. The builder logic lives in the core library, not in each assembly. In Toub’s words, the team wanted “to evolve and improve that logic and have existing previously-compiled binaries just get better”. Underline that sentence. The rest of this article is built on it.
Contexts: What await Carries Along
An await resumes somewhere, and two different mechanisms decide where. The first is the SynchronizationContext (or TaskScheduler) that Part 1 introduced: the awaiter captures it and posts the continuation back, which is exactly what makes textBox.Text = await LoadAsync() work in a UI framework. The second is ExecutionContext, which Toub compared to air: important, but you only notice it when it is missing. It flows ambient data, and the builder’s Start saves and restores it so nothing leaks from an async method into its caller.
ConfigureAwait(false) is the knob for the first mechanism, and only for that one. The ConfigureAwait FAQ is precise about it. The method is not special to the compiler or the runtime, it just returns a struct that wraps the task, and with false the await “pretends as if there isn’t” a context to go back to. The guidance that follows is easy to remember: no ConfigureAwait(false) in app-level code, ConfigureAwait(false) in general-purpose library code. ASP.NET Core has no SynchronizationContext, yet the FAQ is explicit that the library guidance still stands. And the knob does not touch the second mechanism:
static readonly AsyncLocal<string> RequestId = new AsyncLocal<string>();
static async Task ContextAsync()
{
RequestId.Value = "req-42";
await Task.Yield();
await Task.Delay(10).ConfigureAwait(false);
Console.WriteLine("AsyncLocal after awaits: " + RequestId.Value); // req-42
}
AsyncLocal<T> (.NET 4.6) flows as part of ExecutionContext, “regardless of whether ConfigureAwait is used”, unless you explicitly call ExecutionContext.SuppressFlow(). The same split explains the familiar deadlock: block on .Result inside a single-threaded context, and the only thread that could run the continuation is the one you are blocking. I saw that often enough in my consulting days to keep a soft spot for the FAQ.
.NET Core 2.1: The Rewrite Underneath
The first suspension of a .NET Framework async method did a lot of work. Toub lists it: capture the ExecutionContext, allocate a MoveNextRunner to hold it, create an Action delegate to that runner and box the struct. Together with the Task, that is at least four allocations on the first suspension, plus at least two more on each later one when an AsyncLocal is in play. Four allocations, just to say “be right back”. It was a sound design for 2012, when the goal was to make asynchronous code almost as straightforward as synchronous code.
.NET Core 2.1 (2018) rewrote that machinery, and the library-resident builder paid off. Instead of boxing the struct, the core library’s builder uses a generic box that is the Task itself and holds the state machine as a typed field. Known awaiters from CoreLib can treat that box directly as the continuation, so no Action is needed. The 2.1 performance post puts it plainly: “The four allocations have been reduced to one, and the total bytes allocated has shrunk by half.” Separately, the synchronous fast path got cheaper. Toub’s AsyncMethodAwaitInvocation benchmark invokes an async method that completes synchronously and awaits it. It measured 20.36 ns on .NET Core 2.0 and 13.48 ns on .NET Core 2.1, both at 0 B, and the post credits that cut to just-in-time (JIT) compiler and thread-static changes rather than to the new box.
The most striking comparison is in How Async/Await Really Works. One million suspensions (a thousand awaits of a method that itself does await Task.Yield() a thousand times, with an AsyncLocal set) produced more than 5 million allocations totaling about 145 MB on .NET Framework, and about 1,000 allocations totaling about 109 KB on .NET Core. That is not a tuning step. That is a different weight class. The post does not state the .NET Core version, so I will not guess one. Both are Microsoft’s numbers from Toub’s machine, not mine. And because what changed was the library, previously compiled binaries could benefit without being recompiled, which is how Toub describes the design intent.
ValueTask and Channels
Task is built like a minivan. Any number of consumers can await it, you can cache it, you can park it in a dictionary. Toub’s ValueTask post is clear that this flexibility is not needed in the most common case, which is to call something and await it once. Most trips have exactly one passenger. ValueTask<TResult> arrived in layers: as a NuGet package in 2016, with C# 7 generalized async return types in 2017, and in the box with .NET Core 2.0. In .NET Core 2.1 it learned to wrap an IValueTaskSource<TResult>, so a Socket can offer allocation-free receives and sends even when they complete asynchronously (and the Memory<byte> overload of Stream.ReadAsync returns a ValueTask<int>). His rule still stands: the default choice is Task; ValueTask pays off when consumers only await the result directly, allocations matter, and the operation usually completes synchronously or can pool its backing object.
The same plumbing made System.Threading.Channels practical (NuGet in May 2018, in the shared framework since .NET Core 3.0), a producer/consumer queue where WriteAsync and ReadAsync stay cheap when nobody has to wait.
From .NET Core 3.0 to .NET 10: A Staircase
After 2.1 the core async-method transform barely moved (C# 8 added async iterators and C# 10 per-method builders on top of it). What followed was a staircase in the libraries, each step standing on the previous one. The walk below is compressed to what I find most useful in daily work.
.NET Core 3.0: async streams and a cheaper Yield
C# 8 and .NET Core 3.0 combined iterators and async: IAsyncEnumerable<T>, await foreach and async iterators, which Toub says incur at most two allocations of overhead in the common case. Together with channels, they keep pipelines short:
static async IAsyncEnumerable<int> ProduceAsync([EnumeratorCancellation] CancellationToken ct = default)
{
for (int i = 0; i < 3; i++)
{
await Task.Delay(10, ct).ConfigureAwait(false);
yield return i;
}
}
var channel = Channel.CreateBounded<int>(capacity: 2);
Task producer = Task.Run(async () =>
{
await foreach (int i in ProduceAsync())
{
await channel.Writer.WriteAsync(i);
}
channel.Writer.Complete();
});
await foreach (int item in channel.Reader.ReadAllAsync())
{
Console.WriteLine("item " + item);
}
await producer;
The 3.0 performance post shows the 2.1 rewrite paying out again: the box implements IThreadPoolWorkItem, so await Task.Yield() stopped allocating. For 1,000,000 yields Toub measured 581.3 ms on .NET Core 2.1, with Gen0 collections (about 19 per million yields), and 464.4 ms with none on 3.0.
.NET 6: kinder to blocking code, and pooling on request
.NET 6 moved the thread pool’s worker management into managed code and taught it about the most common kind of blocking, waiting on a Task that has not completed. In Toub’s starvation repro a run that took about 32 seconds on .NET Framework 4.8 and .NET 5 finished in about 1.4 seconds (the printed output is 1.36 seconds). His own caveat is worth repeating: “This is not license to start writing more sync-over-async code.” It is respect for teams whose applications cannot move to async all at once, and I have been glad of it on my own teams’ older services.
The same release added PoolingAsyncValueTaskMethodBuilder, applied per method since C# 10. The numbers in that post do not oversell anything. In isolation, 100,000 async ValueTask yields took 763.9 ns per operation without pooling and 781.9 ns with it (about 2% slower), while allocations dropped from 112 B to 0 B. In a 256-way concurrent chain the unpooled run allocated 11,800,058 KB against 214 KB pooled, and took 3.271 s against 2.896 s. Toub explains why pooling is not the default. It changes behavior for code that misuses a ValueTask (awaiting it twice, say), so it belongs where measurements show it helps.
[AsyncMethodBuilder(typeof(PoolingAsyncValueTaskMethodBuilder<>))]
static async ValueTask<int> ReadAsync(int delayMs)
{
await Task.Delay(delayMs);
return delayMs;
}
// .NET 6: wait with a timeout in one call, throws TimeoutException (a CancellationToken overload exists too)
await Task.Delay(Timeout.Infinite).WaitAsync(TimeSpan.FromMilliseconds(50));
.NET 6 also brought Parallel.ForEachAsync and PeriodicTimer. Task.WaitAsync, shown above, replaces a fairly common pattern of combining a task with a timeout or a cancellation token, one that, in Toub’s words, developers often get wrong.
.NET 8 and 9: options, smaller boxes and WhenEach
.NET 8 extended ConfigureAwait with an options enum. ConfigureAwaitOptions adds SuppressThrowing and ForceYielding, and TimeProvider lets tests fake time instead of sleeping. The box lost another 16 bytes on 64-bit (in the .NET 8 performance post, the YieldOnce benchmark went from 112 B to 96 B and from 918.6 ns to 865.8 ns, .NET 7 against .NET 8), and tasks for default(TResult) of 1, 2, 4, 8 and 16-byte value types are now cached.
Task failing = Task.FromException(new InvalidOperationException("boom"));
await failing.ConfigureAwait(ConfigureAwaitOptions.SuppressThrowing); // observe, do not rethrow
await Task.CompletedTask.ConfigureAwait(ConfigureAwaitOptions.ForceYielding); // always hop to the pool
.NET 9 added System.Threading.Lock (with C# 13 lock support) and Task.WhenEach, which solves something Toub says the team wanted to solve “over a decade ago”. Consuming tasks as they complete used to mean a WhenAny loop with List.Remove, an O(N^2) algorithm. With three tasks that finish after 300, 100 and 200 ms (Delayed just awaits Task.Delay and returns a label), the new API is a plain loop:
Task<string>[] tasks = [Delayed("one", 300), Delayed("two", 100), Delayed("three", 200)];
await foreach (Task<string> done in Task.WhenEach(tasks))
{
Console.WriteLine(await done);
}
// two after 100 ms, three after 200 ms, one after 300 ms
In the .NET 9 performance post, N = 1,000 took 20,082.683 μs with the WhenAny loop and 102.759 μs with WhenEach (for N = 10 it was 3.232 μs against 1.223 μs). Roughly 195 times faster at N = 1,000. That is the kind of speedup you usually only get by deleting code.
.NET 10: more resilience and async LINQ
.NET 10 closed a gap from the other side. When a pool thread is about to block on a Task, it now moves its local queue to the global queue first. In Toub’s repro .NET 9 hung (timed out after about 20 seconds) and .NET 10 completed in 4 ms. Channels switched to linked-list waiter queues. In a loop of canceled reads, the working set grew from about 31 MB to about 1.5 GB on .NET 9 and stayed near 46 MB on .NET 10. And System.Linq.AsyncEnumerable joined the framework, replacing, as the breaking-change note puts it, the community-maintained System.Linq.Async package that carried async LINQ for years.
Measuring the Baseline Against Today
Microsoft’s tables come from Toub’s machines, so I wanted numbers of my own. The suite builds a recursive async method five levels deep, in which each level awaits the next and adds 1, and the leaf returns our familiar 42 (outside the runtime’s small-integer task cache, so every completed Task<int> is a real object):
public static async Task<int> SyncTask(int depth)
{
if (depth == 1)
{
return 42;
}
return await SyncTask(depth - 1) + 1;
}
SyncChainTask completes synchronously at every level. SyncChainValueTask is the same chain with ValueTask<int>. YieldChainTask has a leaf that does await Task.Yield(), so all five levels really suspend and resume. The same C# source files were compiled by the .NET 11 SDK once per target framework, so this is four builds, not one binary on four runtimes. The caveats first. These are micro-benchmarks on a hybrid P/E-core laptop (Intel Core Ultra 7 155H, Windows 11), run with BenchmarkDotNet 0.15.8 on .NET Framework 4.8 (a net48 build running on the installed 4.8.1 runtime, CLR 4.8.10008), .NET 8.0.34, .NET 10.0.15 and .NET 11.0.0. The .NET Framework 4.8 job is the baseline. On .NET Framework, ValueTask comes from the System.Threading.Tasks.Extensions 4.6.3 NuGet package, not from the framework itself.
One label needs a sentence of explanation. It turned into a paragraph. My benchmark assembly contains compiler-generated state machines on every runtime in this suite, and the benchmark logs this at startup (“state-machine (5 async methods, 0 flagged Async, 5 IAsyncStateMachine types)”; the helper class also holds two chains used later in the series). On .NET 11 the benchmark code therefore has the same shape as on every other runtime, while most .NET 11 shared-framework libraries, such as System.Private.CoreLib and System.Net.Http, are already compiled in a different way. These chains call no async methods in the libraries (Task.Yield, the builders and the awaiters are ordinary methods), so the label describes my application code. I call that job “.NET 11 (app code: state machines)”. What that difference means is the subject of the final part of this series.
Mean time and allocated memory per call:
| Method | .NET Framework 4.8 | .NET 8 | .NET 10 | .NET 11 (app code: state machines) |
|---|---|---|---|---|
SyncChainTask | 544.7 ns / 401 B | 112.0 ns / 360 B | 88.2 ns / 360 B | 90.8 ns / 360 B |
SyncChainValueTask | 604.9 ns / 0 B | 80.7 ns / 0 B | 60.0 ns / 0 B | 53.1 ns / 0 B |
YieldChainTask | 4,340.4 ns / 1,253 B | 1,375.1 ns / 517 B | 1,320.0 ns / 517 B | 1,354.9 ns / 517 B |
Reading the numbers
Dividing the baseline mean by each newer mean gives these speedups for the same source code: SyncChainTask 4.9x on .NET 8, 6.2x on .NET 10 and 6.0x on .NET 11; SyncChainValueTask 7.5x, 10.1x and 11.4x; YieldChainTask 3.2x, 3.3x and 3.2x. Allocations fall from 401 B to 360 B (about 10%) in the synchronous Task chain and from 1,253 B to 517 B (about 59%) in the yield chain. The 360 B is exactly five 72-byte Task<int> objects, one per level, which a quick check with GC.GetAllocatedBytesForCurrentThread confirms. In the yield chain, the 2.1 box and the leaner awaiter paths described above are part of this gap. The synchronous chains never box, so their gain comes from other improvements in the runtimes underneath, and I made no attempt to isolate the individual steps.
Time for the oddities. On .NET Framework the ValueTask chain (604.9 ns) is a little slower than the Task chain (544.7 ns) even though it allocates nothing, and remember that it is the package version of the type. Among the newer runtimes, I do not read a ranking into .NET 10 against .NET 11 in this run (88.2 against 90.8 ns and 60.0 against 53.1 ns): their 99.9% intervals overlap, in the ValueTask case by a fraction of a nanosecond. .NET 8, on the other hand, trails both in the two synchronous chains by roughly 20 to 28 ns, and its 99.9% error intervals do not overlap with those of .NET 10.
I would still want reruns before crediting the runtime: BenchmarkDotNet flagged five jobs as multimodal, the .NET 11 yield job has a larger standard deviation (98 ns against 22 ns on .NET 10), and everything ran in one session on a hybrid CPU. The baseline is noisy too, and BenchmarkDotNet called the .NET Framework SyncChainTask distribution bimodal. The story that survives every caveat is the baseline against everything that came after it. A chain that really suspends went from 1,253 B and 4.3 μs to 517 B and 1.3 to 1.4 μs, without a change in the source code.
Where a Compiler-Only Design Ends
Look at the hand-written machine above once more. It is a very good answer to the question “how do we get asynchronous code without changing the runtime?”, and every async-method optimization in this article lives inside that answer: a smaller box, a cached task, a pooling builder, a leaner awaiter path. Each one was found by working with the shape the compiler emits.
That shape is also the natural boundary. The runtime only ever sees the result: an ordinary type implementing IAsyncStateMachine, with a MoveNext method, a state integer and an awaiter field. My own startup log shows one compiler-generated type per async method. A five-level chain is five independent instances of one such type, each with its own builder and its own task. They are connected only through the tasks they return (and, once something suspends, through continuations on those tasks). That is why the synchronous Task chain still allocates 360 B although nothing ever suspended. To the runtime it is five unrelated objects, not one logical call stack. A compiler-only design can be exactly as good as the library helpers beneath it, and the .NET teams have spent a decade polishing them. But it can only polish the walls of its room. It cannot see through them.
I am grateful for this design. It carried my own teams’ code from .NET Framework to modern .NET with the same keywords, and it kept getting faster along the way. If you want something practical to do with this part, measure your hot async paths with [MemoryDiagnoser] on the runtimes you actually run, and reach for ValueTask or pooling builders only where the numbers justify it. The most interesting question is still open. What happens when the runtime itself takes part in await?

Comments