Samuele Pilleri
Samuele Pilleri

Reputation: 764

Empty If Optimization

Consider the following:

int status = 0;

while(status < 3)
{
    switch(status)
    {
        case 0:
            // Do something
            break;

        case 1:
            if(cond1 && cond2 || cond3 && cond4)
                ; // status = 1
            else if(cond5)
                status = 2;
            else
                status = 0;
            // there could be more else-if statements
            break;

        case 2:
            // Do something else
            break;
    }
    status++;
}

Given that the first if-statement is only there for the sake of readability and that, as showed, its body is empty (because redundant), I was wondering how could a compiler (either a ahead-of-time or just-in-time compiler) possibly optimize this (if any optimization is possible).

Upvotes: 0

Views: 50

Answers (1)

Cine
Cine

Reputation: 4402

For a general purpose compiler, the answer is no.

Two optimizations that are closely related however, are the Data flow optimizations, which aims to eliminate double calculations and impossible paths in code. Another is Currying, which aims to optimize a general function with regards to the specific parameter.

Upvotes: 1

Related Questions