Without writing `final`, how do `private`, `fileprivate` and whole-module optimization let the Swift compiler call class methods directly?
answer
- proving no override exists
- how much code the compiler sees
- one file versus the whole module
- the access level it can never close
basics
~20 sThe compiler can call a class method directly once it sees every possible override. Private and fileprivate members limit that search to one file; whole-module optimization widens it to the module, covering internal and non-open public members.
solid answer
~40 sDevirtualizing a class method means proving that no override exists anywhere it could be written. A `private` or `fileprivate` member can only be overridden in its own file, so even file-by-file compilation can check that file and **infer `final`**. An `internal` member can be overridden in any file of the module, and by default `swiftc` compiles files separately, so the call stays a vtable call. With **whole-module optimization** (`-whole-module-optimization`) the compiler compiles all files together, sees every subclass, and infers `final` for internal members, and for `public` members that are not `open`, that nothing overrides. An `open` member can be overridden by other modules, so it is never inferred final. A client module calling your public class still sees a possibly overridden method; only `final` lets it call directly.
code
swift · 11 linesclass Canvas {
private func clip() -> Int { 1 } // only this file can see it: no override possible
fileprivate var scale = 2 // same: read as a direct field access
func area() -> Int { 3 } // internal: another file may override it
func render() -> Int {
clip() + scale + area()
}
}
print(Canvas().render()) // prints 6go deeper
Recall that the compiler may call a class method directly when it can see that nothing overrides it, and that private members and whole-module builds make that easier.
Explain why file-by-file compilation keeps internal methods dynamic, how WMO lets the compiler see every subclass, and why open members are never inferred final.
Verify devirtualization in optimized SIL, know it stops at the module boundary for non-final members, and mark public classes final where clients call them in hot paths.
Weigh whole-module builds' longer compile times against the runtime win, and decide which public classes are designed for subclassing and which should be final for clients.
## The question the optimizer must answer A call to a non-final class method normally goes through the class's **vtable**, because a subclass might override it. The optimizer may replace that lookup with a **direct call**, a step called **devirtualization**, only when it can prove there is exactly one implementation the call could reach. The proof depends on how much code the compiler can see and on where an override could legally be written. Access control decides the second part; the compilation mode decides the first. ## What each access level lets the compiler prove | Access of the member | Where an override can be written | Inferred `final`, file by file | Inferred `final`, whole module | |---|---|---|---| | `private`, `fileprivate` | the same file | yes, if the file holds no override | yes | | `internal` (the default) | any file of the module | no | yes, if no file overrides it | | `public` but not `open` | any file of the module | no | yes, if no file overrides it | | `open` | any module | no | no | Swift's optimization guide puts the first row this way: restricting a declaration to its file "allows the compiler to be able to ascertain all other potentially overriding declarations", so it can "infer the `final` keyword automatically". Only `open` classes can be subclassed, and only `open` members overridden, outside their module; a `public` member that is not `open` therefore behaves like `internal` here. Two separate facts combine in every row. The **access level** fixes the set of files where an override could appear. The **compilation mode** decides whether the compiler is looking at all of those files at once. Inference succeeds only when the second covers the first and none of those files actually overrides the member. ## Whole-module optimization By default `swiftc` compiles each file as its own unit, which lets the build compile files in parallel and recompile only what changed. **Whole-module optimization (WMO)** compiles all of a module's sources as one unit, enabled with `-whole-module-optimization` on the command line or through Xcode's build setting for it, which projects usually turn on for release builds. The guide's trade-off: such builds "will most likely take longer to compile, but may run faster". Because `internal` is the default access level, WMO gives app code this devirtualization without any annotation, for every method nothing overrides. Checked with the Swift 6.4 compiler at `-O`: - file by file, a `private` method and a `fileprivate` property of an internal class are called and read directly, while an internal method stays a `class_method` vtable call; - with WMO, the same internal method becomes a direct, inlined call when no subclass overrides it; - with WMO, a `public` method of a non-`open` class is devirtualized, while an `open` method stays a vtable call. ## What inference cannot do - It cannot remove dispatch from a **polymorphic call site**. If a subclass really overrides the method, a call through the base type must still choose at run time. - It does not cross **module boundaries** for non-final members. A client module calling a method of your public, non-open class cannot see your internal subclasses, so it keeps the vtable call; a `final` class or method lets the client call directly. - It never applies to `open` members, or to `@objc dynamic` members, which are always dispatched through the Objective-C runtime. - It is an optimization, not a language rule: an unoptimized `-Onone` build keeps the vtable calls. ## Checking what the compiler did 1. Build the module with `-O`, once with and once without `-whole-module-optimization`. 2. Add `-emit-sil` and search the output for `class_method #YourClass.method`. 3. A remaining `class_method` is a vtable call; a `function_ref`, or no call at all, means the call was devirtualized and possibly inlined. ## Why `final` is still worth writing Inference depends on the build mode and on the absence of overrides today. An explicit `final` holds in every build, turns a future override into a compile error rather than a silent slowdown, and is the only way to let other modules call a public class's methods directly. The fragility is easy to miss: adding one subclass anywhere in the module that overrides a method quietly turns every devirtualized call to that method back into a vtable call in the next whole-module build. Nothing warns, because nothing is wrong; only a profile or the optimized SIL shows it. Where a method sits on a hot path, the explicit keyword makes that intent survive later changes.
- If whole-module optimization already infers `final` for a Swift class, is writing `final` still worth it?Yes. Inference depends on the build mode and on nothing overriding the method today; `final` holds in every build, including file-by-file debug builds, and turns a future override into a compile error. It is also the only way to let other modules call a public class's methods directly, because a client cannot see your internal subclasses.
- Does whole-module optimization of your library let a client module devirtualize calls to your public classes?Not for non-final methods. WMO covers the module being compiled. A client compiling against your interface cannot rule out an internal subclass in your library, so a call to a public, non-open class's method stays a vtable call in the client. Marking the class or the method `final` is what lets the client emit a direct call.
- Internal class methods are always called directly because internal is the default
- Whole-module optimization lets client modules inline every public method
- private only hides a method; it has no effect on dispatch
- An open method gets inferred final if no subclass exists yet
- Devirtualization also happens in -Onone debug builds