11

I have googled this question to clear some basic concepts but didn't find a suitable answer for this.

How many optimization levels are available for code generation in Xcode build setting for the Swift compiler and Apple LLVM (Objective-C)?

Animesh
  • 497
  • 5
  • 17

2 Answers2

14

Swift provides four different optimization levels:

-Onone:

This is meant for normal development. It performs minimal optimizations and preserves all debug info.

-O:

This is meant for most production code. The compiler performs aggressive optimizations that can drastically change the type and amount of emitted code. Debug information will be emitted but will be lossy.

-Ounchecked:

This is a special optimization mode meant for specific libraries or applications where one is willing to trade safety for performance. The compiler will remove all overflow checks as well as some implicit type checks. This is not intended to be used in general since it may result in undetected memory safety issues and integer overflows. Only use this if you have carefully reviewed that your code is safe with respect to integer overflow and type casts.

-Osize:

This is a special optimization mode where the compiler prioritizes code size over performance.

You can ready more about these here: OptimizationTips

Mukesh
  • 2,792
  • 15
  • 32
1

Optimization Level (GCC_OPTIMIZATION_LEVEL) and Optimization Level(SWIFT_OPTIMIZATION_LEVEL)

[Xcode build process]

Objective-C GCC_OPTIMIZATION_LEVEL

It is a part of IR Generation and Optimization AST -> LLVM IR

  • None [-O0](by default for debug) - not optimized code for debug
  • Fast [-O, O1] - between -O0 and -O2 simple optimizations with minimum compile time
  • Faster [-O2] - better optimizations and higher compile time
  • Fastest [-O3] - [-O2] + optimizations without compile time or code footprint limits. the best optimizations and highest compile time
  • Fastest, Smallest [-Os](by default for release) - [-O2] + optimizations for reduce code size
  • Fastest, Aggressive optimization [-Ofast] - [-O3] + aggressive optimizations which can violate strict language compliance
  • Smallest, Aggressive Size Optimizations [-Oz] - [-Os] + optimizations for redusing code footprint

Swift SWIFT_OPTIMIZATION_LEVEL

It is a part of IR Generation and Optimization[About]

  • -Onone(by default for app debug) - minimal optimizations(debug) - build time up, performance down
  • -O - maximum optimizations(by default for app release) - build time down, performance up
    • -O -whole-module-optimization - compiler look at all the source files in a module(except single source file at time). For example execution a function from not the same file(in module scope) will be faster
  • -Osize - size over perfomance

//Additional

  • -Ounchecked - performance over safety

SWIFT_DISABLE_SAFETY_CHECKS

Disable Safety Checks(SWIFT_DISABLE_SAFETY_CHECKS) (by default is false) - Disable runtime safety checks when optimizing is enabled(SWIFT_OPTIMIZATION_LEVEL == -O, -Osize) (-Ounchecked) and it is not taken into account with -Onone

[-O vs -O -whole-module-optimization]

yoAlex5
  • 29,217
  • 8
  • 193
  • 205