Skip to content

Commit 71fc5bd

Browse files
gottesmmsaeta
andauthored
[opt-tips] Remove -Ounchecked and talk about WMO + 'internal' to reduce dynamic dispatch. (#30068)
* Remove reference to -Ounchecked. * Add internal + wmo in reducing dynamic dispatch. * Fix some grammar Co-Authored-By: Brennan Saeta <[email protected]> Co-authored-by: Brennan Saeta <[email protected]>
1 parent e50096f commit 71fc5bd

File tree

1 file changed

+21
-11
lines changed

1 file changed

+21
-11
lines changed

docs/OptimizationTips.rst

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,13 @@ Enabling Optimizations
2121
======================
2222

2323
The first thing one should always do is to enable optimization. Swift provides
24-
four different optimization levels:
24+
three different optimization levels:
2525

2626
- ``-Onone``: This is meant for normal development. It performs minimal
2727
optimizations and preserves all debug info.
2828
- ``-O``: This is meant for most production code. The compiler performs
2929
aggressive optimizations that can drastically change the type and amount of
3030
emitted code. Debug information will be emitted but will be lossy.
31-
- ``-Ounchecked``: This is a special optimization mode meant for specific
32-
libraries or applications where one is willing to trade safety for
33-
performance. The compiler will remove all overflow checks as well as some
34-
implicit type checks. This is not intended to be used in general since it may
35-
result in undetected memory safety issues and integer overflows. Only use this
36-
if you have carefully reviewed that your code is safe with respect to integer
37-
overflow and type casts.
3831
- ``-Osize``: This is a special optimization mode where the compiler prioritizes
3932
code size over performance.
4033

@@ -43,8 +36,8 @@ In the Xcode UI, one can modify the current optimization level as follows:
4336
...
4437

4538

46-
Whole Module Optimizations
47-
==========================
39+
Whole Module Optimizations (WMO)
40+
================================
4841

4942
By default Swift compiles each file individually. This allows Xcode to
5043
compile multiple files in parallel very quickly. However, compiling
@@ -55,8 +48,11 @@ mode is enabled using the ``swiftc`` command line flag
5548
``-whole-module-optimization``. Programs that are compiled in this
5649
mode will most likely take longer to compile, but may run faster.
5750

58-
This mode can be enabled using the Xcode build setting 'Whole Module Optimization'.
51+
This mode can be enabled using the Xcode build setting 'Whole Module
52+
Optimization'.
5953

54+
NOTE: In sections below, for brevity purposes, we will refer to 'Whole
55+
Module Optimization' by the abbreviation 'WMO'.
6056

6157
Reducing Dynamic Dispatch
6258
=========================
@@ -168,6 +164,20 @@ assuming ``E``, ``F`` do not have any overriding declarations in the same file:
168164
return f.myPrivateVar
169165
}
170166

167+
Advice: If WMO is enabled, use 'internal' when a declaration does not need to be accessed outside of module
168+
---------------------------------------------------------------------------------------------------------
169+
170+
WMO (see section above) causes the compiler to compile a module's
171+
sources all together at once. This allows the optimizer to have module
172+
wide visibility when compiling individual declarations. Since an
173+
internal declaration is not visible outside of the current module, the
174+
optimizer can then infer `final` by automatically discovering all
175+
potentially overridding declarations.
176+
177+
NOTE: Since in Swift the default access control level is ``internal``
178+
anyways, by enabling Whole Module Optimization, one can gain
179+
additional devirtualization without any further work.
180+
171181
Using Container Types Efficiently
172182
=================================
173183

0 commit comments

Comments
 (0)