A Swift render loop calls `draw()` on shapes held as a class hierarchy, as `[any Shape]` and through a generic function; where does each call's dispatch cost come from, and how do you remove it?
answer
- what is known per element
- a slot per class, a table per conformance
- one concrete type per array
- a closed set of cases
basics
~20 sClass elements dispatch through the vtable and [any Shape] elements through each value's witness table, because the type varies per element; a specialized generic over one type calls directly. An enum over a closed set of shapes gives direct, inlinable calls.
solid answer
~50 sIn `[ShapeNode]`, subclasses override `draw()`, so each call is a **vtable** lookup that the optimizer keeps: the call site is genuinely polymorphic, and marking a subclass `final` does not help a call made through the base type. In `[any Shape]`, each element is an **existential container** carrying its own witness table, so every call opens the container and goes through that **witness table**. A generic `render<S: Shape>(_ items: [S])` calls through a witness table passed once per call of `render`; when the optimizer specializes it for a concrete `S`, the calls become direct and inlinable, but the array must then hold one type. To remove per-element dispatch, model a closed set of shapes as an **enum** whose `draw()` switches over cases, or group elements into one array per concrete type. Confirm in an optimized build: `-emit-sil` shows `class_method` and `witness_method` where dispatch survives.
code
swift · 26 linesprotocol Shape { func draw() -> Int }
struct Square: Shape { var side: Int; func draw() -> Int { side * side } }
struct Dot: Shape { func draw() -> Int { 1 } }
class ShapeNode { func draw() -> Int { 0 } }
final class SquareNode: ShapeNode { override func draw() -> Int { 4 } }
enum AnyShape {
case square(Square), dot(Dot)
func draw() -> Int {
switch self {
case .square(let s): return s.draw()
case .dot(let d): return d.draw()
}
}
}
func renderNodes(_ items: [ShapeNode]) -> Int { items.reduce(0) { $0 &+ $1.draw() } }
func renderExistentials(_ items: [any Shape]) -> Int { items.reduce(0) { $0 &+ $1.draw() } }
func renderGeneric<S: Shape>(_ items: [S]) -> Int { items.reduce(0) { $0 &+ $1.draw() } }
func renderEnum(_ items: [AnyShape]) -> Int { items.reduce(0) { $0 &+ $1.draw() } }
print(renderNodes([ShapeNode(), SquareNode()])) // 4: vtable call per element
print(renderExistentials([Square(side: 2), Dot()])) // 5: witness table per element
print(renderGeneric([Square(side: 3)])) // 9: direct once specialized
print(renderEnum([.square(Square(side: 2)), .dot(Dot())])) // 5: switch, direct callsgo deeper
Recall that class arrays dispatch through a vtable and protocol arrays through witness tables, and that dispatch is cheapest when the concrete type is known.
Explain why a polymorphic call site cannot be devirtualized, where an existential keeps its witness table, and how a specialized generic becomes direct.
Confirm surviving dispatch in optimized SIL or a Release profile, then pick an enum, per-type arrays or batching, weighing the closed set an enum imposes.
Weigh an open, extensible shape model against a closed, faster one, and decide where a hot path may give up extensibility for predictable performance.
## Three storage forms, three kinds of knowledge A render loop that calls `draw()` thousands of times per frame makes the dispatch mechanism visible, because the method bodies are small and the call overhead is a large share of the work. What the compiler can do depends on what it knows about each element's type at the call site. | Storage | What the compiler knows | Mechanism per call (Swift 6.4, `-O`) | |---|---|---| | `[ShapeNode]` with overriding subclasses | a `ShapeNode` or some subclass | vtable (`class_method`) | | `[any Shape]` holding different structs | some type conforming to `Shape` | witness table (`witness_method`) | | `render<S: Shape>(_ items: [S])` called with `[Square]` | after specialization, exactly `Square` | direct, inlined | | `[AnyShape]`, an enum with a case per shape | a fixed set of cases | `switch` plus direct, inlined calls | ## Why the class array stays dynamic When subclasses really override `draw()`, the call through a `ShapeNode` reference must choose an implementation at run time. Devirtualization only removes dispatch when exactly one implementation is possible, so: - `final` on a subclass does not help: the call's static type is the base class; - `final` on the base method is impossible while subclasses override it; - whole-module optimization sees the overrides and keeps the `class_method` call. Each call loads the class's vtable slot and makes an indirect jump, and the loop body cannot be inlined across it. Checked with the Swift 6.4 compiler: with `final class SquareNode: ShapeNode` overriding `draw()`, the optimized loop over `[ShapeNode]` still contains its `class_method` call. ## Why the existential array stays dynamic An `any Shape` value is an **existential container**: an inline buffer holding the value (or a pointer to a heap copy when it is too large), the type's metadata and a pointer to the `Shape` witness table for that type. Because each element may be a different type, the call opens the container and jumps through that element's table. Copying values in and out of the container, and the heap boxes for large values, are a separate cost that belongs with existential storage costs; the dispatch itself is one indirect call per element that the optimizer cannot inline. ## The generic function Unspecialized, `render<S: Shape>` receives the `Shape` witness table for `S` as a hidden argument and calls `draw()` through it. Within the module, the optimizer usually creates a **specialized** copy for each concrete type it sees `render` called with, and in that copy `draw()` is a direct, inlinable call. (When specialization happens, and why it stops at module boundaries, is a compiler-optimization topic of its own.) The catch: a generic array holds one type, so a scene with several shape types needs several arrays. ## Ways to remove the per-element dispatch 1. **Enum for a closed set.** If the shapes are known and owned by one module, an `enum AnyShape { case square(Square), dot(Dot) }` whose `draw()` switches over `self` turns every call into a branch plus a direct call to a concrete method, which the optimizer can inline. 2. **Group by concrete type.** Keep `[Square]` and `[Dot]` and loop over each with concrete or generic code, so each inner loop is monomorphic. 3. **Final classes where polymorphism is not needed.** If only one class is ever stored, make it `final` and store that type. 4. **Batch at the protocol boundary.** A requirement such as `drawAll()` on a per-type collection pays one dynamic call per group instead of one per element. Each choice gives something up: an enum closes the set of shapes, so adding one means editing every `switch`, and plugins from other modules cannot add cases; a protocol or class hierarchy stays open to extension. ## Measure before redesigning - Profile a **Release** (`-O`) build; `-Onone` keeps the vtable and witness-table calls the optimizer would remove and exaggerates the cost. - Run `xcrun swiftc -O -emit-sil` on the module and search for `class_method` and `witness_method` inside the loop's function to confirm which calls survived. - Dispatch matters when the method body is tiny and the loop is hot; when `draw()` does real work, the indirect call is noise and the open design may be worth more. A useful way to present the result in a review is to name, for each storage form, which instruction remains in the loop and what design change would remove it. That keeps the discussion on evidence from the optimized build rather than on general claims that classes or protocols are slow.
- In a Swift loop over `[ShapeNode]`, would marking the subclass `SquareNode` as `final` remove the vtable call?No. The loop calls `draw()` through the static type `ShapeNode`, whose method is overridden, so the runtime must still choose between implementations. `final` on `SquareNode` only makes calls whose static type is `SquareNode` direct. Removing the dispatch needs a design where the call site knows the concrete type, such as a homogeneous array or an enum.
- When is replacing a Swift protocol or class hierarchy with an enum the wrong fix for dispatch cost?When the set of shapes must stay open. An enum is closed: every new shape means a new case and an edit to every `switch`, and other modules cannot add cases. If shapes come from plugins or other teams' modules, keep the protocol, and remove dispatch by grouping elements per concrete type or by batching calls instead.
- Marking each subclass final removes the vtable call from a loop over the base class
- Whole-module optimization devirtualizes every call in a class hierarchy
- Calls on [any Shape] are dispatched through a vtable, like classes
- A generic function always uses a witness table, even in a release build
- Dispatch overhead dominates every loop, whatever the method body does