...

/

Understanding Various Levels of Optimizations

Understanding Various Levels of Optimizations

Learn how to utilize the emcc compiler’s optimization options and integrate the Closure Compiler to enhance code efficiency and reduce code size.

C/C++ programs are compiled and converted into native code via Clang or the GCC compiler. They convert the C/C++ program based on the target. Here, the target refers to the end machine where the code is executed. emcc has the Clang compiler built in. The emcc compiler is responsible for converting the C or C++ source code into LLVM byte code.

In this section, we’ll see how to improve the optimization and code size of the generated WebAssembly binary code. To improve the efficiency and generated code size, the Emscripten compiler has the following options:

  • Optimizations

  • Closure Compiler

Let’s talk about optimizations first.

Optimizations

The goal of the compiler is to reduce the cost of compilation—that is, the compile time. With the -O optimization flag, the compiler tries to improve the code size and/or the performance at the expense of the compile time. In terms of compiler optimizations, code size and performance are mutually exclusive. The faster the compile time, the lower the optimization. To specify the optimization, we use the -O<0/1/2/3/s/z> flag. Each of the options includes various assertions, code size optimizations, and code performance optimizations, along with others.

The following are the various optimizations available:

  • -O0: This is the default option and a ...