Skip to content

Avoid unnecessary async state machine generation #9871

@hez2010

Description

@hez2010

We have plenty of code like below:

async IAsyncOperation<T> Foo()
{
    return await AsyncInfo.Run(async () => 
    {
        // no await in the body at all
        return foo;
    });
}

which doesn't actually await anything in an async method. Instead, we should rewrite them to

IAsyncOperation<T> Foo()
{
    return Task.FromResult(foo).AsAsyncOperation();
}

This can avoid unnecessary async state machine be generated to reduce GC pressure.

Metadata

Metadata

Assignees

Labels

good first issueGood issue for new contributors to get started with

Projects

Status

✅ Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions