Method Dispatch in Swift: The Complete Guide
How does Swift *really* execute function calls?
If you’re like me, you’ve worked with a grizzled staff engineer that generously doles out helpful nitpicks to improve the performance of your code.
Perhaps, like me, you’ve also been that grizzled engineer.
You know the sort.
Nitpicks like “this method should be declared fileprivate”, or “keep your generics in the same file where they’re used”, and the all-time classic “mark this function as final” to help out the compiler and speed up your code.
In modern Swift, you might as well spend your time writing branch prediction hints for the compiler. That is, the compiler and runtime do a ton of optimisation under the hood that makes these tips redundant.
These optimisations are mostly around Method Dispatch.
That is, how Swift executes function calls. This knowledge is crucial for understanding the low-level performance characteristics of your code.
Today we’re going to learn:
How (and why) Swift implements all 4 types of dispatch
What the Swift compiler does to your methods in secret
How the Swift runtime calls into your functions at runtime
How to make your code run faster
How to build an intuition about method dispatch
Upgrade to read this article right now, or wait until January 5.
Paid members get several benefits:
🌟 Access Elite Hacks, my exclusive advanced content
🚀 Read my free articles a month before anyone else
🧵 Master concurrency with my full course and advanced training
Keep reading with a 7-day free trial
Subscribe to Jacob’s Tech Tavern to keep reading this post and get 7 days of free access to the full post archives.


