Method Dispatch
What is it?
method calling
dispatching
The process of finding a
called method in memory
method execution
Provides developers with optimization options to improve the
performance of the program
There are 3 types of method dispatch - static dispatch, table
dispatch (divided into witness table and virtual table) and
message dispatch
details and features of each type
Method Dispatch
Static Dispatch
Fastest method dispatch
At compile time it is known which method will be called
There is only one implementation of this method in memory
The method cannot be overridden
1. Value types 2. Extensions
3. Reference type with keywords static and final
4. Attribute private applied to all class or specific methods
💡 the compiler sees that the
method cannot be overridden and
makes the dispatch static
Method Dispatch
Table Dispatch
Method implementation is chosen at runtime instead of compile-time
(slower than static dispatch)
Needed to support inheritance, polymorphism, and protocols
Witness Table
for protocols, supports polymorphism, does not support inheritance
in existential container
it stored takes 5 machine words
Value Witness Table
How
we will omit the details of its work
em ory?
m allocate ...
age s copy
i
...
ata destruct ...
3 machine
e d re
ur he
words is a deallocate ...
value t
c ed
buffer
ru r
st sto
color
pointer to Protocol Witness Table
CatSoundable
Value Witness Table
makeSound 0x001 (address)
pointer to
Protocol Witness Table storing methods in memory is
associated with a specific instance,
not with a protocol
if the structure supported several
protocols, there would be pointers
to others witness tables
The address of the method is taken from the table of a specific instance in runtime
Witness tables allows different types to conform to the same protocol and have
their own implementations for the protocol methods
Method Dispatch
Virtual Table
for classes, supports polymorphism and inheritance
superclass table
0x00A Cat
0x001 sleep
0x002 makeSound
subclass table
0x00B Lion
0x001 sleep
if a method is overridden
0x102 makeSound its address in the table is
changed
0x103 singHakunaMatata
Each class has a virtual table that contains references to the implementations
of its methods
Even if a variable is declared with a superclass type, the actual implementation
of a method will be called based on the subclass type 👇
Method Dispatch
A common task in an interview
Be careful with the method implementation in extensions
Method Dispatch
Message Dispatch
The most dynamic, but also the slowest
Usually not used in Swift, only in ObjC
For use in swift we must use the dynamic modifier
Allows method swizzling
superclass table
0x00A Cat
0x001 sleep
0x002 makeSound
subclass table
0x00B Lion
0x00A super
0x102 makeSound
0x103 singHakunaMatata
1. We need to call a sleep() method from Lion class, we try to find this
method in the Lion table, but it is not there.
2. We go it to the superclass table and looking for a method there.
3. Do this until the function pointer is found or the root class of the
hierarchy is reached
📝
Method Dispatch
Initial Declaration Extension
Value Type Static Static
Reference Type Table (Virtual) Static
often we can optimize to static
Protocol Table (Witness) Static
NSObject Table (Virtual) Message