Why inheritance is class-only in Swift - Performance Analysis
We want to understand how the cost of using inheritance in Swift grows as programs get bigger.
Specifically, why inheritance works only with classes (not structs) and what that means for performance: constant-time dynamic dispatch.
Analyze the time complexity of method calls in a class inheritance chain.
class Animal {
func speak() {
print("Animal sound")
}
}
class Dog: Animal {
override func speak() {
print("Bark")
}
}
let pet = Dog()
pet.speak()
This code shows a simple class inheritance where Dog inherits from Animal and overrides a method. Swift uses dynamic dispatch via vtables.
Look at what happens when methods are called through inheritance.
- Primary operation: Dynamic method dispatch using the class's virtual method table (vtable).
- How many times: Once per method call; vtable lookup is constant time, independent of hierarchy depth.
Method dispatch time does not grow with inheritance depth thanks to vtables.
| Inheritance Depth (n) | Approx. Operations |
|---|---|
| 1 | 1 vtable lookup |
| 5 | 1 vtable lookup |
| 10 | 1 vtable lookup |
Pattern observation: The cost is constant (O(1)) regardless of the depth of the inheritance chain.
Time Complexity: O(1)
This means the time to dispatch a method is constant, even as the inheritance chain gets longer.
[X] Wrong: "Method dispatch in class inheritance walks the hierarchy linearly, taking O(n) time."
[OK] Correct: Swift classes use vtables for O(1) dynamic dispatch. Structs lack inheritance because they are value types without reference identity or dynamic dispatch, avoiding copying issues and enabling static dispatch via protocols.
Explaining O(1) dispatch and why inheritance is class-only shows deep knowledge of Swift's type system, value vs. reference semantics, and performance trade-offs.
"Why can't structs inherit? How would adding dynamic dispatch to structs impact performance compared to classes?"