NEW: My new book Pro SwiftUI is out now – level up your SwiftUI skills today! >>

What’s the performance cost of calling an async function?

Paul Hudson    @twostraws   

Updated for Xcode 14.2

Whenever we use await to call an async function, we mark a potential suspension point in our code – we’re acknowledging that it’s entirely possible our function will be suspended, along with all its callers, while the work completes. In terms of performance, this is not free: synchronous and asynchronous functions use a different calling convention internally, with the asynchronous variant being slightly less efficient.

The important thing to understand here is that Swift cannot tell at compile time whether an await call will suspend or not, and so the same (slightly) more expensive calling convention is used regardless of what actually takes place at runtime.

However, what happens at runtime depends on whether the call suspends or not:

  • If a suspension happens, then Swift will pause the function and all its callers, which has a small performance cost. These will then be resumed later, and ultimately whatever performance cost you pay for the suspension is like a rounding error compared to the performance gain provided by async/await even existing.
  • If a suspension does not happen, no pause will take place and your function will continue to run with the same efficiency and timings as a synchronous function.

That last part carries an important side effect: using await will not cause your code to wait for one runloop to go by before continuing.

It’s a common joke that many coding problems can be fixed by waiting for one runloop tick to pass before trying again – usually seen as DispatchQueue.main.async { … } in Swift projects – but that will not happen when using await, because the code will execute immediately.

So, if your code doesn’t actually suspend, the only cost to calling an asynchronous function is the slightly more expensive calling convention, and if your code does suspend then any cost is more or less irrelevant because you’ve gained so much extra performance thanks to the suspension happening in the first place.

Hacking with Swift is sponsored by Essential Developer

SPONSORED From March 20th to 26th, you can join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer!

Click to save your free spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Similar solutions…

Buy Pro Swift Buy Pro SwiftUI Buy Swift Design Patterns Buy Testing Swift Buy Hacking with iOS Buy Swift Coding Challenges Buy Swift on Sundays Volume One Buy Server-Side Swift Buy Advanced iOS Volume One Buy Advanced iOS Volume Two Buy Advanced iOS Volume Three Buy Hacking with watchOS Buy Hacking with tvOS Buy Hacking with macOS Buy Dive Into SpriteKit Buy Swift in Sixty Seconds Buy Objective-C for Swift Developers Buy Beyond Code

Was this page useful? Let us know!

Unknown user

You are not logged in

Log in or create account

Link copied to your pasteboard.