This page shows the source for this entry, with WebCore formatting language tags and attributes highlighted.
Title
Don't <c>return await</c> unless you have to
Description
I finally got around to verifying that the defining dependent async methods like the following one is wasteful.
<code>
public async Task<bool> N()
{
return await M();
}</code>
<img attachment="async-await.jpg" align="right">A less-contrived example looks like this:
<code>using System.Threading.Tasks;
public class C {
public Task<bool> M()
{
return Task.FromResult(false);
}
public async Task<bool> N()
{
return await M();
}
public async void RunIt()
{
var result = await N();
}
}</code>
This yields something like the following lowered C# code in <a href="https://sharplab.io/">SharpLab.IO</a>. Note that there are two state machines.
<code>public Task<bool> M()
{
return Task.FromResult(false);
}
[AsyncStateMachine(typeof(d__1))]
[DebuggerStepThrough]
public Task<bool> N()
{
d__1 stateMachine = new d__1();
stateMachine.<>t__builder = AsyncTaskMethodBuilder<bool>.Create();
stateMachine.<>4__this = this;
stateMachine.<>1__state = -1;
stateMachine.<>t__builder.Start(ref stateMachine);
return stateMachine.<>t__builder.Task;
}
[AsyncStateMachine(typeof(<runit>d__2))]
[DebuggerStepThrough]
public void RunIt()
{
<runit>d__2 stateMachine = new <runit>d__2();
stateMachine.<>t__builder = AsyncVoidMethodBuilder.Create();
stateMachine.<>4__this = this;
stateMachine.<>1__state = -1;
stateMachine.<>t__builder.Start(ref stateMachine);
}</code>
If you write the equivalent code without the <c>await</c> in the <c>N</c> method:
<code>using System.Threading.Tasks;
public class C {
public Task<bool> M()
{
return Task.FromResult(false);
}
public Task<bool> N()
{
return M();
}
public async void RunIt()
{
var result = await N();
}
}</code>
...then you get the following lowered C#. Note that now there is only one state machine.
<code>public Task<bool> M()
{
return Task.FromResult(false);
}
public Task<bool> N()
{
return M();
}
[AsyncStateMachine(typeof(<runit>d__2))]
[DebuggerStepThrough]
public void RunIt()
{
<runit>d__2 stateMachine = new <runit>d__2();
stateMachine.<>t__builder = AsyncVoidMethodBuilder.Create();
stateMachine.<>4__this = this;
stateMachine.<>1__state = -1;
stateMachine.<>t__builder.Start(ref stateMachine);
}</code>