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

后端 未结 3 871
北海茫月
北海茫月 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:01

    Every method is going to have a state machine, yes.

    Keep in mind that the "overhead" of the state machine is primarily the allocation of one object (that and a few gotos, which are going to be quite fast), so any type of "optimization" that you perform to remove it is the same as not creating an instance of a class once.

    As to whether or not it's cost is greater or less than doing the work synchronously, that's something you're going to need to do performance benchmarks on given the specifics of your application and hardware to know for sure.

提交回复
热议问题