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