Skip to content

Task.Delay 100x slower on mono than coreclr on macOS #57877

Open

Description

@trympet

Description

On macOS, the overhead of Task.Delay is 100x higher on mono.

The behavior has been observed on:
2017 MacBook w/ Intel i5 x86
2020 Mac Mini w/ M1 arm64

Steps to reproduce:

  1. dotnet new macos (requires .NET 6) or create from VS template.
  2. Open AppDelegate.cs and add the following:
        public override void DidFinishLaunching(NSNotification notification)
        {
            // Insert code here to initialize your application
        _ = Task.Run(TaskBenchmark);
        _ = Task.Run(TaskBenchmark);
        _ = Task.Run(TaskBenchmark);
        _ = Task.Run(TaskBenchmark);
        }

        public static async Task TaskBenchmark()
        {
            int i = 0;
            while(i++ < 10000) {
                await Task.Delay(1).ConfigureAwait(false);
            }
        }
  1. Open top, Activity Monitor, etc., and observe CPU usage of the process.

For reference, the coreclr equivalent is waay more performant.

  1. Create a new .NET 5 console app from a template.
  2. dotnet new globaljson; set SDK to 5.0.9.
  3. Add the following and build with dotnet build -c Release /p:UseAppHost=True
    public static void Main(string[] args)
    {
        _ = Task.Run(TaskBenchmark);
        _ = Task.Run(TaskBenchmark);
        _ = Task.Run(TaskBenchmark);
        _ = Task.Run(TaskBenchmark);
        Console.ReadKey();
    }

    public static async Task TaskBenchmark()
    {
        int i = 0;
        while(i++ < 10000) {
            await Task.Delay(1);
        }
    }

This completely breaks benign methods which rely on TPL for frequent polling.

Expected behavior

The CPU time should be equivalent or better on Mono than on coreclr.

Actual behavior

CPU time is much greater than what is reasonable for a wait method, causing hangs and freezes.

Configuration

OS=macOS Big Sur 11.5.2 (20G95) [Darwin 20.6.0]
Apple M1 2.40GHz, 1 CPU, 8 logical and 8 physical cores
Intel Core i5-7Y54 CPU 1.20GHz (Max: 1.30GHz) (Kaby Lake), 1 CPU, 4 logical and 2 physical cores
.NET SDK=5.0.400

Data

Coreclr:
image

Mono:
image

Activity

  1. added
    tenet-performance
    on Aug 21, 2021
  2. ghost added
    untriaged
    on Aug 21, 2021
  3. changed the title [-]Task.Delay 100x slower on mono than coreclr for macOS[/-] [+]Task.Delay 100x slower on mono than coreclr on macOS[/+] on Aug 21, 2021
  4. EgorBo commented on Aug 22, 2021

    @EgorBo
    Member

    I'd guess it's not Task.Delay that is slow but the machinery around async/await (state machines) handling

  5. trympet commented on Aug 22, 2021

    @trympet
    ContributorAuthor

    I'd guess it's not Task.Delay that is slow but the machinery around async/await (state machines) handling

    Yes, sounds right. Using a plain old System.Threading.Timer provides acceptable perf.

    However, the issue still remains; libraries which uses async await over explicit threading (e.g. render timers, async logging, etc.) can have widely different perf characteristics between Mono and coreclr. Having to fork every library which suffers because of this is less than ideal when multi-targeting.

  6. removed
    untriaged
    on Aug 23, 2021
  7. added this to the 7.0.0 milestone on Aug 23, 2021
  8. SamMonoRT commented on Aug 23, 2021

    @SamMonoRT
    Member

    @lambdageek @vargaz - marked for 7.0, but if there is a low risk fix, we can backport

  9. modified the milestones: 7.0.0, 8.0.0 on Aug 4, 2022
  10. modified the milestones: 8.0.0, 9.0.0 on Jul 19, 2023
  11. modified the milestones: 9.0.0, Future on Jul 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions