Will every 'await' operator result in a state machine?

后端 未结 3 869
北海茫月
北海茫月 2020-12-17 23:51

Please consider the following code:

public async Task GetString()
{
    //Some code here...
    var data = await A();
    //Some more code...
          


        
3条回答
  •  南方客
    南方客 (楼主)
    2020-12-18 00:17

    Will every method get translate into a state machine? Or is the compiler sophisticated enough to generate a more efficient structure?

    No, the compiler will generate a state-machine for each of these calls. The compiler doesn't check the semantical call-chain of your methods. It will generate a state-machine on a method basis only.

    You can see that clearly when looking at the compiled code:

    Compiler generated code:

    does that affect the trade-off between the async-await overhead (such as the state machine) and the non-blocking thread benefits?

    You will have to test your code in order to be able to say that. Generally, async IO is good when you need through-put. If your async methods are going to be hit concurrently by multiple callers, you'll be able to see the benefits. If not, you might not see any effect of a performance gain. Again, benchmark your code.

提交回复
热议问题