swiftlang / swift

The Swift Programming Language
https://swift.org
Apache License 2.0
67.37k stars 10.34k forks source link

Async/await: slow in certain combinations #60444

Open stefanspringer1 opened 2 years ago

stefanspringer1 commented 2 years ago

Describe the bug Without suspension happening, asynchronous calls should be about as fast as synchronous calls. But this is not always the case.

Steps To Reproduce Steps to reproduce the behavior:

  1. Run the Swift package https://github.com/stefanspringer1/AsyncBenchmark.

Expected behavior Without suspension happening, asynchronous calls should be about as fast as synchronous calls.

What happens instead But in certain circumstances, more precisely for the last three runs in the example, the factor is much higher, up to more than 6000. (In debug mode, only the last number is much higher than 1, it is 22).

Profile data Attached: profile.trace.zip

Program output (release):


--------------------------

100000 iterations
Evaluating: Execution...
Time for Execution: 0.0001037 seconds
Evaluating: AsyncExecution...
Time for AsyncExecution: 0.0001041 seconds
100000 iterations done
factor: 1.0040209
difference: 0.0000004

--------------------------

10000 iterations
Evaluating: Execution...
Time for Execution: 0.0000105 seconds
Evaluating: AsyncExecution...
Time for AsyncExecution: 0.0000105 seconds
10000 iterations done
factor: 0.9960952
difference: -0.0000000

--------------------------

1000 iterations
Evaluating: Execution...
Time for Execution: 0.0000011 seconds
Evaluating: AsyncExecution...
Time for AsyncExecution: 0.0000010 seconds
1000 iterations done
factor: 0.9603321
difference: -0.0000000

--------------------------

100 iterations
Evaluating: Execution...
Time for Execution: 0.0000001 seconds
Evaluating: AsyncExecution...
Time for AsyncExecution: 0.0000001 seconds
100 iterations done
factor: 0.6640000
difference: -0.0000000

--- directly in main 1 ---

100000 iterations
Evaluating: Execution...
Time for Execution: 0.0000516 seconds
Evaluating: AsyncExecution...
Time for AsyncExecution: 0.0119533 seconds
100000 iterations done
factor: 231.5407651
difference: 0.0119017

===== NON-ASYNC WORK =====

--------------------------

100000 iterations
Evaluating: Execution...
Time for Execution: 0.0000838 seconds
Evaluating: AsyncExecution...
Time for AsyncExecution: 0.0035407 seconds
100000 iterations done
factor: 42.2342486
difference: 0.0034568

--- directly in main 2 ---

100000 iterations
Evaluating: Execution...
Time for Execution: 0.0000419 seconds
Evaluating: AsyncExecution...
Time for AsyncExecution: 0.4371535 seconds
100000 iterations done
factor: 10429.0254312
difference: 0.4371115
Program ended with exit code: 0

Environment (please fill out the following information)

Additional context Add any other context about the problem here.

stefanspringer1 commented 2 years ago

Also See Forum topic https://forums.swift.org/t/async-await-slow-in-certain-combinations/59497/3.