Swift in Production. Method Dispatch Mechanisms.

Mobile Apps Development A-Z Guide.

Dmytro Nasyrov
Pharos Production
4 min readMay 3, 2017

--

Give us a message if you’re interested in Blockchain and FinTech software development or just say Hi at Pharos Production Inc.

Or follow us on Youtube to know more about Software Architecture, Distributed Systems, Blockchain, High-load Systems, Microservices, and Enterprise Design Patterns.

Pharos Production Youtube channel

PREFACE

One day you can ask yourself why C is blazingly fast and his cousin Objective-C has not the best performance? One of the answers is because of Method Dispatch. What’s that method dispatch? That’s how your application invokes methods from memory before starts to execute them on CPU. So what techniques of method calling is the most popular now?

DIRECT DISPATCH

This type is used by C and it’s the fastest. No dynamic stuff at all. The compiler knows what to call and when already while building. You can inject code directly during compilation(inline) and there are different performance improvements etc. The only one thing we should remember here is — Direct Dispatch make your app runs faster but you can’t use subclasses.

TABLE DISPATCH

This is the most popular type of dispatch across current languages. Java, Perl, JS and many other of them use it as default dispatch method. It works quite straightforward, every class has a table with pointers on functions. Every subclass contains its own copy of such table with all new methods on the bottom of the table. It’s impossible to inject in this table, only append to the bottom. That’s why categories and extensions don’t work here. In runtime, the app uses this table to understand where is the required method. While compiler can optimize Direct Dispatch calls, it can’t optimize Table Dispatch. So it’s much slower than Direct.

DYNAMIC DISPATCH

It’s the slowest one. When you create a class which is a subclass of another class which is a subclass of some other class and app will decide at runtime what method of which parent it should use — this is Dynamic Dispatch. Slow but runtime powerful. You can do different stuff like Key-Value Observing and Method Swizzling(hope you don’t really do this) just because you have Dynamic Dispatch in your compiler’s arsenal. To call a function program should dig into the class checking one by one all of its methods to find a required one. Of course, performance is low, even with the help of different caches.

SWIFT

In Swift compiler uses all 3 dispatch mechanisms.

  • Value types always use Direct Dispatch. Structs, Enums, Contiguous Arrays. You can’t use them withing objective-c as is. They are fast, almost all swift’s stuff is done with value types.
  • Protocol extensions use Table Dispatch for required protocol methods and Direct Dispatch for not required.
  • Classes — Table Dispatch for class methods, Dynamic Dispatch for required extension’s methods and Direct Dispatch for not required.
  • All NSObject methods — Table Dispatch, all methods done on top of NSObject — Dynamic Dispatch.

What should we do with all this stuff

There is a bunch of small tricks to make the app run faster.

  • Use final keyword to specify Direct Dispatch for your class or method. You can’t subclasses finalized class.
final keyword
  • Don’t use dynamic keyword. Objective-C runtime will see every dynamic method but you will get Dynamic Dispatch on all of them. One more cool thing about dynamic methods is that you can specify a dynamic method in your extension and then override it in a subclass.
  • Use private keyword. It restricts method declaration to a file and allows a compiler to find overriding declarations and make method final if there is none of them found.
private keyword
  • Use Whole Module Optimization. All declarations with internal access(when no access specified or this type of access specified explicitly) will be compiled as a single module instead of separated modules for each class. This allows to a compiler to use final type on all methods with no overrides.

Thank for reading!

--

--

Dmytro Nasyrov
Pharos Production

We build high-load software. Pharos Production founder and CTO.