2

We all knew Async Await is made possible because of statemachine being put in place, this lead me to a question for following use case:

Public async Task<string>   GetTotal(string p1)
{
    Return await GetTotal(p1,””);
}
Public async Task<string>   GetTotal(string p1, string p2)
{
  Return await GetTotal(p1,””,””);
}

Public async Task<string>   GetTotal(string p1, string p2, string p3)
{
  Return await GetTotal(p1,””,””, “”);
}

Public async Task<string>   GetTotal(string p1, string p2, string p3,string 
p4)
{
 //do some actual async calling
  Return result;
}

If there is no async/await in picture, the above code is absolutely normal, but for async/await, should we avoid coding pattern like what is listed above, basically, should we try to avoid statemachines if we do not have to?

 Public async Task<string>   GetTotal(string p1)
{
  //do some actual async calling
  Return result;
}
Public async Task<string>   GetTotal(string p1, string p2)
{
  //do some actual async calling
  Return result;
}
Public async Task<string>   GetTotal(string p1, string p2, string p3)
{
  //do some actual async calling
  Return result;
} 
Public async Task<string>   GetTotal(string p1, string p2, string p3,string 
p4)
{
 //do some actual async calling
 Return result;
}
yuahorse
  • 21
  • 3
  • 2
    Don't use `await` if you don't actually need it. A function without `await` in its body shouldn't even have `async` applied to it. – xxbbcc May 22 '17 at 13:25
  • 1
    FWIW, I don't see this question and the linked one as exact duplicates, and there is *a lot* that can be said *specifically about the overhead* that is not really relevant (or present) in the other question. – Marc Gravell May 22 '17 at 13:32
  • 1
    I considered reopening but we already have https://stackoverflow.com/q/37861864/ – H H May 22 '17 at 13:39

2 Answers2

0

If you change

public async Task<string> GetTotal(string p1)
{
    Return await GetTotal(p1,””);
}

to

public Task<string> GetTotal(string p1)
{
    return GetTotal(p1,””);
}

(by removing the async and await in functions that don't do async work) you will avoid the unnecessary state machine by just passing the innermost Task object along. This should be done to avoid unnecessary task allocations (and state machines).

Even with the new ValueTask class, having additional async functions that wrap inner functions, there is additional memory allocation / garbage collection cost if this is done in a high throuput code path.

Martin Ullrich
  • 94,744
  • 25
  • 252
  • 217
0

There is overhead on methods that have the async modifier. Where possible, if you can avoid it, save the trouble and return the task.

This is perfectly valid, and is recommended approach. Remove the async keyword, and return the task from the other async method. The state machine will only be generated once (In the GetTotal function):

public Task<string> GetTotal(string p1)
{
  //do some actual async calling
  return result;
}

For further reading, you can read about async/await overhead here

Blue
  • 22,608
  • 7
  • 62
  • 92