What's new for async-await?

Asked on 08/05/2024

1 search

What's New for Async-Await?

At WWDC 2024, several sessions discussed updates and improvements to async-await in Swift. Here are the key points:

  1. Concurrency Model Enhancements:

    • Swift 6 introduces data race safety, which diagnoses data races at compile time. This is a significant improvement for concurrent programming, ensuring safer and more reliable code. This feature is part of the new Swift 6 language mode, which is opt-in (Platforms State of the Union).
  2. Async Function Implementation:

    • Async functions in Swift are implemented by splitting them into multiple partial functions at runtime. This allows the functions to manage their local state on a separate stack from the C stack, improving performance and memory management (Explore Swift performance).
  3. Memory Allocation for Async Functions:

    • Async tasks hold onto one or more slabs of memory, and when an async function needs to allocate memory, it asks the task for memory from the current slab. If the slab is full, a new slab is allocated. This method is typically faster than traditional malloc, providing a performance profile similar to synchronous functions but with slightly higher overhead (Explore Swift performance).
  4. Testing with Async-Await:

    • Swift testing now supports parallel testing by default, and async-await can be used in test code just as in production code. This allows tests to run more efficiently by suspending and yielding the CPU to other tasks while waiting for asynchronous conditions (Go further with Swift Testing).
  5. Structured Concurrency:

    • Swift's concurrency model, including async-await, structured concurrency, and actors, has been progressively refined. These features collectively contribute to the new data race safety in Swift 6, making concurrent programming easier and safer (What’s new in Swift).

Relevant Sessions

These sessions provide a comprehensive overview of the latest advancements in async-await and concurrency in Swift.