Method Dispatch in Swift

Every time, when a method is called in Swift, Method Dispatch is involved. It tells your application where to find the method in memory before it gets executed on the CPU. Especially when writing Swift code it’s very important to understand the concept behind Method Dispatch since it can lead to performance issues when not knowing.

Method Dispatch can be categorised into two different types

  • Static Dispatch
  • Dynamic Dispatch

Static Dispatch

Static Dispatch, also known as Direct Dispatch, is the fastest and most efficient type of method dispatch. Methods that are statically dispatched can be executed immediately at runtime because the compiler knows the exact memory address at compile time. This also means that the compiler can perform various kinds of optimisations such as inlining which leads to even faster execution times at runtime.

If a method is dynamically dispatched, the compiler does not know the exact memory address of the method and can therefore not optimise the code. This can lead to performance issues. However, this also means that Swift allows you to override methods and properties of a superclass and enables functionalities like Polymorphism in Swift.

Dynamic Dispatch

Dynamic Dispatch can be categorised into two different types

  • Table Dispatch
  • Message Dispatch

Table Dispatch is used among the most common approaches in compiled languages to implement this dynamic behaviour. At compile time, a so-called vtable is constructed for each class, which contains an array of function-pointers corresponding to the implementation in each class. Compared to static dispatch, table dispatch needs two additional instructions (read and jump) to determine the method to run at runtime.

Message Dispatch is the most dynamic but also the slowest dispatch technique. In order to find the method behind the dispatch message, the runtime needs to crawl the entire class hierarchy to determine which method to invoke. However, this also means that it is possible to change the behaviour of the program at runtime - which enables techniques like swizzling to work. Objective-C heavily relies on Message Dispatch and also provides that functionality to Swift via the Objective-C runtime.

How to tell the dispatch type?

In order to determine if a method is statically or dynamically dispatched you first need to check if the method is declared inside the initial object or declared as part of an extension.

  Initial declaration Extension declaration
Class Table Static
Value Type Static Static
Protocol Table Static
NSObject Table Message

 

Swift also provides a few different modifiers that alter the dispatch behaviour of a method:

  • dynamic declared methods will be available in the Objective-C runtime and also dispatched by message

  • final all methods on a final declared class will be statically dispatched

  • final @objc declared methods will be available to message dispatch

  • @nonojc declared methods will no longer be available for message dispatch

Written on May 20, 2019