perfomance async/await

Asked on 08/06/2024

1 search

To understand the performance of async/await in Swift, you can refer to the session titled "Explore Swift performance" from WWDC 2024. This session provides an in-depth look at how async functions are implemented and optimized in Swift.

Here are some key points discussed in the session:

  1. Memory Allocation for Async Functions:

    • Async functions keep their local state on a separate stack from the C stack.
    • They are split into multiple partial functions at runtime, each spanning the gaps between potential suspension points.
    • Memory allocation for async tasks is managed using slabs of memory, which can be more efficient than traditional malloc-based allocation.
  2. Partial Functions:

    • An async function is divided into partial functions at each suspension point.
    • Each partial function runs like an ordinary C function until the next suspension point.
    • This approach ensures that only one partial function is on the call stack at any time, improving performance.
  3. Performance Overhead:

    • While async functions have a bit higher overhead for calls compared to synchronous functions, the overall performance profile is similar.
    • The allocator used by async tasks is typically faster than malloc because it is used by a single task and follows a stack discipline.

For a detailed explanation, you can watch the relevant part of the session Explore Swift performance (25:49).

Additionally, the session "A Swift Tour: Explore Swift’s features and design" also touches on the use of async/await in Swift, explaining how task suspension is modeled in code and how the await keyword is used to indicate potential suspension points. You can find this discussion in the chapter on concurrency.

For more information, you can refer to the chapter on concurrency in the session A Swift Tour: Explore Swift’s features and design.

Relevant Sessions

  1. Explore Swift performance
  2. A Swift Tour: Explore Swift’s features and design