Skip to content

Performance regression in .NET Core 3.1 when awaiting Task that completes synchronously? #33133

@CodeBlanch

Description

@CodeBlanch

Consider this simple code:

using System.Threading.Tasks;

namespace Benchmarks
{
    internal static class Program
    {
        public static void Main(string[] args)
        {
            TestClassBenchmark().GetAwaiter().GetResult();
        }

        public static async Task TestClassBenchmark()
        {
            for (int i = 0; i < 5000; i++)
            {
                await WorkerNoParameter();
            }
        }

        private static async Task WorkerNoParameter()
        {
            await NothingWorker();
        }

        private static Task NothingWorker()
        {
            return Task.CompletedTask;
        }
    }
}

Basically a bunch of awaits but everything is really completing synchronously.

That program when run on .NET Core 3.1 allocates the async state machine 5000 times:

image

Same test run on net462 and netcoreapp2.1 do not allocate at all, which was the expected result.

Hopefully I'm mistaken on this and someone can point me in the right direction, but I thought it was worth running up the flag pole just in case.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions