DEV Community

Gustavo Tavares
Gustavo Tavares

Posted on

SPO600 - Week 4

Hey there,

In week 4 we looked on how to compiler optimize our code. It was made clear to us that the way we write code actually will be rewritten by the compiler, so our code gets optimized.

The optimization that the compiler does, most of the time is to make the code achieve the same result but with improved performance, in order to do that it changes our code in certain ways that we will take a look right now.

It is important to say that we, as programmers, should not worry about doing this optimization ourselves, the compiler will automatically do it for us.

About Code Rewriting Optimizations

There is a series of techniques of Code Rewriting Optimizations that the compiler does like: Strength Reduction, Hoisting, Hoisting II - Loop-Invariant Expression, Pre-calculation of Constants and others… More can be seen on our wiki page here.

Strength Reduction

We all know that some operations are more expansive than others, for example a multiplication is more expansive than an addition.
The Strength Reduction is nothing less than replacing a more expansive operation for a cheaper one but that will achieve the same result.

Take a look on the example extracted from https://wiki.cdot.senecacollege.ca/wiki/Compiler_Optimizations#GCC_Optimization_Options

//BEFORE
int x;
for (x=0; x < 10; x++) {
    printf("%d\n", x*6);
}

//AFTER

int x;
for (x=0; x < 60; x+=6) {
    printf("%d", x);
}

Enter fullscreen mode Exit fullscreen mode

Hoisting

Hoisting involves moving an operation outside of the loop.
If that operation inside the loop is resulting in the same result as if it was outside of the loop, the compiler will just move it outside.

//For example
for(int i = 0; i  < 5; i++){

var1=0; 
b=b+5;

}

//Will become: 

var1=0;
for(int i= 0; i < 5; i++){

b=b+5;

}

Enter fullscreen mode Exit fullscreen mode

Hoisting II - Loop-Invariant Expression

Hoisting II - Loop-Invariant Expression will get an expansive calculation inside a loop and bring it outside so it can be made just once, then the result will be placed back on its former place.

For example,

Before:

for(int i = 0; i  < 5; i++){
foo(a, 5*5)
}

//After:
int b = 5*5;
for(int i = 0; i  < 5; i++){
foo(a, b)
}

Enter fullscreen mode Exit fullscreen mode

Pre-calculation of Constants

Pre-calculation of Constants is basically getting a constant expression that will never change and replace it for its result.

See the example from https://wiki.cdot.senecacollege.ca/wiki/Compiler_Optimizations#GCC_Optimization_Options

Before:
ff = (212-32)/100; /* factor for celcius-farenheit conversion */
conv = c * ff + 32;

After:
conv = c * 1.800 + 32;

There is many that you can read on our wiki.

What I learned

The code that I write as a programmer, and the code that will be transformed in machine language are actually different. The compiler will get everything I have done and will optimize it.

The compiler is smarter than me, that’s not hard to see, but its good to know that it will take care of these kind of optimizations for me.

Top comments (0)