Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What kinds of C optimization practices in x86 that were historically recommended to use are no longer effective?

Tags:

c

optimization

Due to the advances in x86 C compilers (namely GCC and Clang), many coding practices that were believed to improve efficiency are no longer used since the compilers can do a better job optimizing the code than humans (e.g. bit shift vs. multiplication).

Which specific practices are these?

like image 224
Mys_721tx Avatar asked Jun 05 '14 20:06

Mys_721tx


People also ask

What are the factors influencing the optimization in compiler design?

Compiler optimizing process should meet the following objectives : The optimization must be correct, it must not, in any way, change the meaning of the program. Optimization should increase the speed and performance of the program. The compilation time must be kept reasonable.

What is compiler optimization in embedded C?

Built-in compiler optimizations include one or more of the following: using options at the command-line and front-end of the tools; compiler-recognized keywords used at the source level (for example, using __packed might tell a compiler to reorder some declared global data in memory to eliminate padding) and automatic ...


1 Answers

Of the optimizations which are commonly recommended, a couple which are basically never fruitful given modern compilers include:

Mathematical transformations

Modern compilers understand mathematics, and will perform transformations on mathematical expressions where appropriate.

Optimizations such as conversion of multiplication to addition, or constant multiplication or division to bit shifting, are already performed by modern compilers, even at low optimization levels. Examples of these optimizations include:

x * 2  ->  x + x x * 2  ->  x << 1 

Note that some specific cases may differ. For instance, x >> 1 is not the same as x / 2; it is not appropriate to substitute one for the other!

Additionally, many of these suggested optimizations aren't actually any faster than the code they replace.

Stupid code tricks

I'm not even sure what to call this, but tricks like XOR swapping (a ^= b; b ^= a; a ^= b;) are not optimizations at all. They're just party tricks — they are slower, and more fragile, than the obvious approach. Don't use them.

The register keyword

This keyword is ignored by many modern compilers, as it its intended meaning (forcing a variable to be stored in a register) is not meaningful given current register allocation algorithms.

Code transformations

Compilers will automatically perform a wide variety of code transformations where appropriate. Several such transformations which are frequently recommended for manual application, but which are rarely useful when applied thus, include:

  • Loop unrolling. (This is often actually harmful when applied indiscriminately, as it bloats code size.)
  • Function inlining. (Tag a function as static, and it'll usually be inlined where appropriate when optimization is enabled.)
like image 77
user149341 Avatar answered Oct 08 '22 18:10

user149341