what's the difference between static in dynamic dispatch?

Generated on 4/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

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

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

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.

A Swift Tour: Explore Swift’s features and design

A Swift Tour: Explore Swift’s features and design

Learn the essential features and design philosophy of the Swift programming language. We’ll explore how to model data, handle errors, use protocols, write concurrent code, and more while building up a Swift package that has a library, an HTTP server, and a command line client. Whether you’re just beginning your Swift journey or have been with us from the start, this talk will help you get the most out of the language.

Consume noncopyable types in Swift

Consume noncopyable types in Swift

Get started with noncopyable types in Swift. Discover what copying means in Swift, when you might want to use a noncopyable type, and how value ownership lets you state your intentions clearly.