Daily Dose of Swift

Method Dispatch

Photo by Arnel Hasanovic on Unsplash

Firstly, we need to remember a very important concept of object orientation which is polymorphism. Polymorphism is the ability to implement a method inherited from a super class in a specialized way.

In Swift, we declare a specialized implementation as an override.

The Method Dispatch, is the mechanism responsible for verifying which method implementation should be performed. They have techniques to perform this verification, which vary in performance and flexibility. Next, we will learn two types of techniques which are Static Dispatch and Dynamic Dispatch.

Static Dispatch

The Static Dispatch is linked to the compilation time of a method derived from a reference type.

A reference type passes to instance a memory reference of itself. This process causes a link between them in memory, a change impacts the value of all instances. In Swift, we have classes as representatives.

During code compilation, static dispatch tells the system to directly access the memory address that the method is allocated to. This process runs quickly and allows optimizations for the compiler, which does not need to do extra work because it already knows which operation to perform. These compiler optimizations are important because they can directly impact the performance of an application.

To specify to the Swift compiler that we want to treat reference type operations as static dispatch, we must use the words final and static. The final word is related to inheritance between classes, and its use specifies that a class cannot be inherited. The word static is the definition that a class property and method cannot be changed by subclasses. Value types, already have a static implementation by default, and do not require this specification.

Value types pass a copy of the information it contains to yours instances, so that they are not linked in memory.In Swift, we have Structs and Enums as representatives.

Protocol and class extensions are also performed with static dispatch by default.

Dynamic Dispatch

The Dynamic Dispatch only has access to an operation during its execution time, therefore, its verification is slower than Static Dispatch. It is divided into two techniques, which are the dispatch table and the dispatch of messages.

In table dispatch, a reference type is associated with the compilation time for a virtual table, which comprises an array of method pointers. The compiler needs the table, whose implementation of the method is being referenced, either the subclass or the superclass. The memory of the objects is only allocated at run time, so that the reading and skipping of the table cannot be optimized, causing loss of performance. In Swift, classes and protocols use sending tables as standard in the initial declaration.

The sending of messages, initiates the referral process provided, and iterates through its hierarchy, until it finds the implementation of the desired method. It is a message dictionary, which can be modified at run time and can change the behavior of the application. Due to its dynamic behavior, the use of message sending can be expensive for an application, although the search performance is not so slow, because of the use of cache. In Swift, for reference types to have dynamism for message dispatch, we need to use Objective-C execution time. This dynamism is achieved by defining the word dynamic, and an @objc tag, in the functions of reference types. In cocoa, we have KVO and Core Data as examples of structures that use messaging as standard.

When to use Static Dispatch or Dynamic Dispatch?

Whenever your class has no methods that make changes at runtime, use static dispatch. Think carefully if your class needs inheritance and if you need to, prefer that it has protocols. If the class is not inherited, use the final word before the class name as stated in the article can improve the performance of your class. And when your function needs to make changes at runtime, mark it with @objc thus calling Objective-C's message dispatch.

Conclusion

As we learned in this article, the Dispatch Method is the mechanism responsible for determining how an operation should be performed, and without it, the Object Orientation paradigm would have no way of existing. Therefore, your understanding is essential so that we programmers can write code with performance and dynamism. All techniques have gains and losses, and must be measured by the context and form of implementation.

That's all folks, we just learned how Method Dispatch works.

Thanks for reading!

📚 References

Dispatch - Apple Developer Documentation