Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What do the optimization levels `-Os` and `-Oz` do in rustc?

Executing rustc -C help shows (among other things):

-C opt-level=val       -- optimize with possible levels 0-3, s, or z 

The levels 0 to 3 are fairly intuitive, I think: the higher the level, the more aggressive optimizations will be performed. However, I have no clue what the s and z options are doing and I couldn't find Rust-related information about them.

like image 700
Lukas Kalbertodt Avatar asked Aug 10 '17 08:08

Lukas Kalbertodt


People also ask

What are the optimization levels in GCC?

-O1 (optimize minimally) -O2 (optimize more) -O3 (optimize even more) -Ofast (optimize very aggressively to the point of breaking standard compliance)

How many levels of optimization are there?

6.4 Optimization levels. In order to control compilation-time and compiler memory usage, and the trade-offs between speed and space for the resulting executable, GCC provides a range of general optimization levels, numbered from 0--3, as well as individual options for specific types of optimization.

What is compiler optimization level?

The degree to which the compiler will optimize the code it generates is controlled by the -O flag. No optimization. In the absence of any version of the -O flag, the compiler generates straightforward code with no instruction reordering or other attempt at performance improvement. -O or -O2.


2 Answers

It seems like you are not the only one confused, as described in a Rust issue. It seems to follow the same pattern as Clang:

  • Os For optimising the size when compiling.
  • Oz For even more size optimisation.
like image 179
Englund Avatar answered Sep 29 '22 20:09

Englund


Looking at these and these lines in Rust's source code, I can say that s means optimize for size, and z means optimize for size some more.

All optimizations seem to be performed by the LLVM code-generation engine.

like image 45
red75prime Avatar answered Sep 29 '22 20:09

red75prime