Static Dispatch Over Dynamic Dispatch

A performance analysis

Shubham Bakshi
Jul 16 · 5 min read
Photo by Robin Pierre on Unsplash

In my previous article, I explained various dispatch techniques available in the Swift programming language. Whenever we hear the term dispatch techniques, the two that come to mind are Static and Dynamic.

I highly recommend that you read my previous article if you haven’t already, so you can get some knowledge of dispatch techniques.

But if you think you know what they are, then let’s proceed!


How It All Started

Long ago, there was an article by Apple, which said Static dispatch outperforms dynamic dispatch. So you should always default to Static dispatch and only shift to Dynamic as and when necessary.

The core reason that we get performance gains when we go for Static dispatch over dynamic is because, in the case of static dispatch, the compiler is aware, at compile time, which method implementation of a certain class is to be called. This way, the compiler can tune in some optimization techniques like setting some flags or maybe, just maybe, if at all possible, convert the call to an inline dispatch (which is the fastest of all)!

Whereas, in the case of Dynamic dispatch, the compiler can only figure out which method implementation is to be called for a certain class at run time, i.e., whether base class method or child class.

This gives Static dispatch a performance gain over Dynamic.


How to Achieve Static Dispatch

Now, as per the article, there are three ways to achieve Static dispatch or reduce Dynamic dispatch:

This allows the compiler to find all potentially overriding declarations. The absence of any such overriding declarations enables the compiler to infer the final keyword automatically and remove indirect calls for methods and property accesses.

What this means is that, whenever we mark the method or variable as private, the compiler performs a search if that method or variable is being overridden anywhere. If it is, it will generate a compile time error. If it doesn’t find any overridden behavior, it will implicitly mark it with final.


And Finally

After I read the article, there was only one thing on my mind — Where’s the proof?

I know we have gone through a lot of theoretical concepts till now, but we are programmers. We love code. And what better proof than a code that proves all those concepts!

A small test

I created a small Performance Tester project, which is nothing but a bunch of Swift classes and some Unit Tests. This was the only way I could find to test the performance/speed of the code. You can clone the repository and see for yourself.

I’ve only used the final keyword, but you can use the other two approaches as well. I’ll tell you in a gist what the project components are and how to run the test cases.

There are just two files that matter to us in the project:

  1. StaticDispatch.swift — This contains all the classes we’ll use for our test cases.
  2. PerformanceTesterTests.swift — This is present under the PerformanceTesterTests group, which is our Unit Tests group and contains our test case.

I have added comments in both files to make it more self-explanatory.

To run a test case —

This will start the iPhone simulator. As soon as the simulator comes online, it will run the test case.

Final class

Some Observations

Class neither final nor subclassed as well
Class not final but is subclassed
A derived (or child) class

So, to sum it all up, as a good practice, you should initially mark your class as final, and if inheritance is required, remove the final keyword. This ensures compiler optimisations and hence improves the code’s performance.

Better Programming

Advice for programmers.

Shubham Bakshi

Written by

iOS Engineer | Motivational Speaker enthusiast

Better Programming

Advice for programmers.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade