[)roi(]
Executive Member
- Joined
- Apr 15, 2005
- Messages
- 6,282
TIL: An really enlightening look into the under covers topic of method dispatching; whilst largely Swift focused it expands on the general topic quite well e.g. Explaining differences between the various types of dispatching methods utilized within C, C++, Java, Objective-C and Swift.
In brief
In brief
https://www.raizlabs.com/dev/2016/12/swift-method-dispatch/Method Dispatch is how a program selects which instructions to execute when invoking a method. It’s something that happens every time a method is called, and not something that you tend to think a lot about. Knowing how method dispatch works is vital when writing performant code, and can illuminate some of the confusing behavior found in Swift.
Compiled programming languages have three primary methods of dispatch at their disposal: direct dispatch, table dispatch, and message dispatch, which I explain below. Most languages support one or two of these. Java uses table dispatch by default, but you can opt into direct dispatch by using the final keyword. C++ uses direct dispatch by default, but you can opt into table dispatch by adding the virtual keyword. Objective-C always uses message dispatch, but allows developers to fall back to C in order to get the performance gains of direct dispatch. Swift has taken on the noble goal of supporting all three types of dispatch. This works remarkably well, but is a source of confusion to many developers, and is behind a number of gotchas that most Swift developers have encountered.