Async/await: slow in certain combinations

In this repository I tested the performance of async/await in comparison to non-async code in a special situation, without anything being suspended. In most cases the factor between async calls and non-async calls is quite low (near 1, sometimes even lower than 1.)

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).

I identified two things that may play a role:

  • Async methods executing a non-async block of code.
  • Async calls being executed right in the main function.

Q: Is this something that can be explained easily, are the large factors pointing to some missing optimization of the Swift compiler, or are those just combinations that should be avoided, because they should not be expected to work efficiently in principle?

(I was using Swift 5.6.1 on macOS 12.5.)

1 Like

In general, if you notice Swift being slow, please gather a time profile in Instruments and file a bug report. In many cases we can just fix it :slight_smile: (for example, having many child tasks used to be very slow and is much faster now)

6 Likes

Bug report: Async/await: slow in certain combinations · Issue #60444 · apple/swift · GitHub.