Swift & Concurrency (50 Qs)•Part 3: Runtime Internals, Swift 6 Safety & Systems Programming (25 - 35)
Method Dispatch in Swift: Static vs Table vs Message Dispatch.
senior LevelswiftRuntime Internals
In Simple Words: How the CPU resolves which function implementation to execute:
- Static / Direct Dispatch (Fastest): Resolved at compile time. The CPU jumps directly to memory address. Used for
finalclasses, structs, enums, and protocol extensions. - Table Dispatch (V-Table & Witness Table): Looked up via function pointer arrays at runtime. Used for standard class inheritance (V-Table) and protocol conformance (Witness Table).
- Message Dispatch (Most Dynamic / Slowest): Uses Objective-C runtime (
objc_msgSend). Supports swizzling and dynamic resolution. Enabled with@objc dynamic.