If statements in c++



Bob shared a great resource on x86 optimization which should stay in a more prominent place than a comment of an issue as it looks like a great read (I only skimmed through, but will read in detail):


The discussion was about the evilness of if statements. While these are certainly to be avoided for code clarity and apparently also speed considerations, I wonder if the following scheme also presents an issue wrt to speed (since branching occurs). Say I want to branch based on the type of some variable and I do things like:

template<typename T>
void foo(T& bar) {
   typedef stan::is_var<T> bar_is_var;
   if(bar_is_var::value) {
      // do stuff for var case
  } else {
     // do stuff vor non-var case

First off, the above is dummy code and would be solved very likely with template special cases. The question I would have if such an if statement would be costly in terms of speed. The thing is that the evaluation is at runtime, but which branch is to be taken is fixed at compile time given the used technique. I would expect that a decent compiler will detect this and generate 100% optimized code, i.e. code which will either always execute the var or non-var case depending on the type of T (without a if statement ever happening at runtime).

Anyone has any clue on this? Is my expectation that code will be properly optimized false?


PS: I am not advocating IFs, they are just sometimes very useful when used with care.


If the condition in the conditional is resolved statically (as in Sebastian’s metaprogramming example), then the compiler will do the right thing and not introduce any run time cost or any space for the non-executed branch. However, the bodies of the conditional both still have to compile. That’s why we had to introduce the complicated operands_and_partials and view structures into the distributions. They do a lot of this conditional execution based on var/non-var status to avoid computing normalizing constants.