This is the second day of my participation in Gwen Challenge

Compiling links is a big part of the system itself, which we’ll explain in a future article.

About compile-time optimization

OC is a compiled language. The built-in compiler of Xcode has been converted from GCC to LLVM. LLVM is divided into front-end Clang and back-end LLVM.

For example,

int a = 5;
int b = 3;
int c = a + b;
Copy the code

These several lines of code may be merged into one line of code after optimization. From the assembly level, it can be clearly seen that the instructions before optimization are shown in the figure.

The optimized instructions after setting are shown in the figure

You can see:

  • Before optimization, read 5 and 3 into the register respectively and proceedaddWrite back to x0 register for use after calculation;
  • After optimization, the result 8 has been calculated in the compilation stage, and the result 8 is directly read into the register for use in the run stage.

Build Settings > Optimization Level on iOS

The official explanation

Specifies the degree to which the generated code is optimized for speed and binary size.

None[-O0]: Do not optimize. With this setting, the compiler’s goal is to reduce the cost of compilation and to make debugging produce the expected results. Statements are independent: if you stop the program with a breakpoint between statements, you can then assign a new value to any variable or change the program counter to any other statement in the function and get exactly the results you would expect from the source code. Fast[-O1]: Optimizing compilation takes somewhat more time, and a lot more memory for a large function. With this setting, the compiler tries to reduce code size and execution time, without performing any optimizations that take a great deal of compilation time. In Apple’s compiler, strict aliasing, block reordering, and inter-block scheduling are disabled by default when optimizing. Faster[-O2]: The compiler performs nearly all supported optimizations that do not involve a space-speed tradeoff. With this setting, the compiler does not perform loop unrolling or function inlining, or register renaming. As compared to the ‘Fast’ setting, this setting increases both compilation time and the performance of the generated code. Fastest[-O3]: Turns on all optimizations specified by the ‘Faster’ setting and also turns on function inlining and register renaming options. This setting may result in a larger binary. Fastest, Smallest[-Os]: Optimize for size. This setting enables all ‘Faster’ optimizations that do not typically increase code size. It also performs further optimizations designed to reduce code size. Fastest, Aggressive Optimizations[-Ofast]: This setting enables ‘Fastest’ but also enables aggressive optimizations that may break strict standards compliance but should work well on well-behaved code.

Chinese translation

  • None[-o0]: not optimized. In this setting, the compiler’s goal is to reduce compilation costs and ensure that the desired results are produced during debugging. The program statements are independent: if the program stops at a breakpoint on a line, we can assign a new value to any variable or point the program counter to any statement in the method, and get a run result that is exactly the same as the source code.
  • Fast[-o1]: Large functions require slightly increased compilation time and memory consumption. In this setting, the compiler tries to reduce the size of the code file and the execution time, but does not perform optimizations that require a lot of compile time. In Apple’s compiler, strict aliases, block rearrangements, and scheduling between blocks are disabled by default during optimization.
  • Faster[-O2]: The compiler performs all supported optimization options that do not involve time-space swapping. In this setting, the compiler does not loop unwrap, function inlining, or register renaming. This setting increases compilation time and generated code performance compared to the ‘Fast[-o1]’ option.
  • Fastest[-O3]: Enabled function inlining and register rename options while enabling all optimizations supported by ‘Fast[-O1]’. This setting may cause the binaries to become larger.
  • Fastest, Smallest[-OS]: Optimizes the size. This setting enables all optimizations in ‘Fast[-o1]’ that do not increase the code size, and further optimizations that reduce the code size are performed.
  • Slowest, Aggressive Optimizations[-ofast]: This setting enables all of the Optimizations in ‘Fastest[-O3]’, as well as Aggressive Optimizations that break strict compilation standards but do not affect code that works well.

The default Debug mode is None[-o0] and the default Release mode is Fastest [-OS]. In real projects, the optimization level can be set according to the situation of the project.

It should be noted that any optimization is based on the consideration of time and space. The increase of speed is often accompanied by the increase of space cost, and vice versa.