Does async/await have an extra cost associated when returning early from the method compared to the synchronous method?
Take these examples:
public async Task<bool> TryDoSomethingAsync()
{
if ( Is99PercentOfTheCases() )
return false; // does this path...
await DoSomethingAsync();
return true;
}
// versus
public bool TryDoSomething()
{
if ( Is99PercentOfTheCases() )
return false; // ...have the same cost as this path?
DoSomething();
return true;
}
I know async/await has an extra cost associated and therefore you need to be careful around tight loops - see i.e. https://www.red-gate.com/simple-talk/dotnet/net-framework/the-overhead-of-asyncawait-in-net-4-5/
But when is this cost occurring?
- Is it when you mark a method as async?
- Is it when it needs to await?
- Or is it related to the Task return type?
- Especially: does the performance hit still occur when returning early for an async method?
In the end it's always best to profile specific cases, but I'm interested in the theory behind it.