what's the difference between static in dynamic dispatch?
Asked on 04/17/2025
1 search
Static dispatch and dynamic dispatch are two different methods of function call resolution in programming, particularly in Swift, as discussed in the Explore Swift performance session at WWDC 2024.
-
Static Dispatch: This occurs when the function to be called is determined at compile time. It is more efficient because it allows for optimizations like inlining and generic specialization. The compiler can see the function definition and optimize the call accordingly. Static dispatch is used when the function is declared in a protocol extension.
-
Dynamic Dispatch: This occurs when the function to be called is determined at runtime. It is less efficient than static dispatch because it doesn't allow for the same level of compile-time optimizations. However, dynamic dispatch enables polymorphism and other powerful tools for abstraction. In Swift, dynamic dispatch is used when a function is a protocol requirement, meaning it is declared in the main body of the protocol.
Understanding the difference between these two types of dispatch is crucial for optimizing performance and leveraging Swift's capabilities effectively.

Explore Swift performance
Discover how Swift balances abstraction and performance. Learn what elements of performance to consider and how the Swift optimizer affects them. Explore the different features of Swift and how they’re implemented to further understand the tradeoffs available that can impact performance.

Migrate your app to Swift 6
Experience Swift 6 migration in action as we update an existing sample app. Learn how to migrate incrementally, module by module, and how the compiler helps you identify code that’s at risk of data races. Discover different techniques for ensuring clear isolation boundaries and eliminating concurrent access to shared mutable state.

Go small with Embedded Swift
Embedded Swift brings the safety and expressivity of Swift to constrained environments. Explore how Embedded Swift runs on a variety of microcontrollers through a demonstration using an off-the-shelf Matter device. Learn how the Embedded Swift subset packs the benefits of Swift into a tiny footprint with no runtime, and discover plenty of resources to start your own Embedded Swift adventure.