Whole-module optimization is an optimization mode of the Swift compiler. The performance win of whole-module optimization heavily depends on the project, but it can be up to two or even five times.
Even the simple return statement in
getElementneeds a lookup in the type’s metadata to figure out how to copy the element. It could be a simple
Int, but it could also be a larger type, even involving some reference counting operations. The compiler just doesn’t know.
Function specialization and inlining across files are just examples of optimizations the compiler is able to do with whole-module optimizations. Even if the compiler decides not to inline a function, it helps a lot if the compiler sees the implementation of the function. For example it can reason about its behavior regarding reference counting operations. With this knowledge the compiler is able to remove redundant reference counting operations around a function call.
The second important benefit of whole-module optimization is that the compiler can reason about all uses of non-public functions. Non-public functions can only be used within the module, so the compiler can be sure to see all references to such functions.
Stay up-to-date by subscribing to the Comments RSS Feed for this post.