@@ -21,20 +21,13 @@ Enabling Optimizations
21
21
======================
22
22
23
23
The first thing one should always do is to enable optimization. Swift provides
24
- four different optimization levels:
24
+ three different optimization levels:
25
25
26
26
- ``-Onone ``: This is meant for normal development. It performs minimal
27
27
optimizations and preserves all debug info.
28
28
- ``-O ``: This is meant for most production code. The compiler performs
29
29
aggressive optimizations that can drastically change the type and amount of
30
30
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.
38
31
- ``-Osize ``: This is a special optimization mode where the compiler prioritizes
39
32
code size over performance.
40
33
@@ -43,8 +36,8 @@ In the Xcode UI, one can modify the current optimization level as follows:
43
36
...
44
37
45
38
46
- Whole Module Optimizations
47
- ==========================
39
+ Whole Module Optimizations (WMO)
40
+ ================================
48
41
49
42
By default Swift compiles each file individually. This allows Xcode to
50
43
compile multiple files in parallel very quickly. However, compiling
@@ -55,8 +48,11 @@ mode is enabled using the ``swiftc`` command line flag
55
48
``-whole-module-optimization ``. Programs that are compiled in this
56
49
mode will most likely take longer to compile, but may run faster.
57
50
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'.
59
53
54
+ NOTE: In sections below, for brevity purposes, we will refer to 'Whole
55
+ Module Optimization' by the abbreviation 'WMO'.
60
56
61
57
Reducing Dynamic Dispatch
62
58
=========================
@@ -168,6 +164,20 @@ assuming ``E``, ``F`` do not have any overriding declarations in the same file:
168
164
return f.myPrivateVar
169
165
}
170
166
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
+
171
181
Using Container Types Efficiently
172
182
=================================
173
183
0 commit comments