Increase App Performance (Method Dispatch iOS)

Mina Ghali
3 min readDec 24, 2022

--

App performance is one of the most important key elements that measures app quality and affects the user experience. It is affected by many things, one of them is Method Dispatch and how to reduce Dynamic Dispatch which will be tackled below. The article starts by discussing how a program executes, what is Method Dispatch and its types and how to reduce the Dynamic Dispatch.

How Does a Program Execute?

When a code is written and compiled, the compiler will turn the high level language to machine level language and as a part of this process it tries to keep track of the location of the functions used in the code. To do that, the compiler uses an array of functions, that is known as (Witness table or Virtual table), which will be discussed later through the article.

What is method dispatch?

Dispatch means sending. Thus, Method Dispatch tells the application where to find the method in the memory before it gets executed in the CPU.

What are Method Dispatch Types?

1- Static Dispatch (Direct Dispatch)

2- Dynamic Dispatch (Table Dispatch)

3- Message Dispatch

Below is the explanation of Static and Dynamic Dispatch:

1- Static Dispatch:

Static Dispatch is the fastest dispatch as the compiler already knows the location of the function so it is directly executed.

Static Dispatch is only possible if there is no inheritance. Thus, it is concluded that the value type data structures have static dispatch, so if a comparison took place between structs and classes, the structs have a static dispatch. Although the static dispatch is very fast, it restricts subclassing.

2- Dynamic Dispatch

Dynamic Dispatch takes place if there is inheritance or subclassing. So with reference type data structures like class, the compiler makes an array of function pointers (witness table or virtual table). And when a function is invoked, its address is searched in the witness table at runtime and then the invocation happens. However, it is comparatively slower than the static dispatch.

So, Method Dispatch is understood along with its types. In addition, it is now known that the Static Dispatch is faster than the Dynamic Dispatch. Accordingly, the dynamic dispatch has to be reduced in order to increase the performance of the application.

Some Examples below:

struct NormalStruct {//Static Dispatch
}
class NormalClass {//Dynamic Dispatch
func anything() {}
}
final class NormalClass {//Static Dispatch
func anything() {}
}
extension NormalClass {//Static Dispatch
func anything() {}
}

From the previous example it is concluded that:

  • extensions use Static Dispatch.
  • Keyword final makes the class use Static Dispatch because it ensures that there is no inheritance or subclassing.
  • Value types like struct use static dispatch.

Hint: if you will not inherit from a class, use final or make the methods private to be Static Dispatch.

Here is Another Example:

func anything(parameter: AnyProtocol) {// Dynamic Dispatch
//Do somthing
}
func anything<T: AnyProtocol>(parameter: T) {// Static Dispatch
//Do somthing
}

Most of the developers use the function with protocol parameters which is an Existential type. An Existential type is an abstract type that can hold any value of any conforming type dynamically, using Existential type in Swift has a serious effect on the app’s performance.

Why Does this Happen?

In the Existential types, Swift needs to make some processes at runtime, which makes the functions’ address be searched in the witness table at runtime and then the invocation happens. This scenario is Dynamic Dispatch. However, in the generic type, the compiler already knows the location of the function and it directly executes it. This scenario is Static Dispatch.

Furthermore, it can be concluded that functions with protocol-parameters use Dynamic Dispatch and generic parameters with constraints use Static Dispatch.

Hint: To solve this and to increase the performance by using Static Dispatch, use generic type with constraint of protocol type, instead of protocol type only.

Conclusion

To increase the app’s performance, you can do some small additions to the code to reduce using Dynamic Dispatch. So, the following keywords could be used: final, private, extensionand generic parameters instead of protocol parameters to reach Static Dispatch for a better performance.

Reference

https://www.rightpoint.com/rplabs/switch-method-dispatch-table

--

--