0

My question is very much related to this question but something remained unclear for me after reading that question and very good answer.

So as I understand it, the processor executing the code makes a branch prediction such that it already does the processing towards one branch in the hope that branch will be the one that needs to be executed. If it is wrong it will have to reroll some instructions which takes time. As I understand it the prediction is based on the pattern in the past.

So now my question would be: if I know for sure there will be a pattern in what the if statement will evaluate to, is that just as fast when the decision would have been made at compile time?

More specific for my case, I have a template function and I could do template specialization such that the decision is made on compile time, but in my opinion the code will be more clear if I use std:is_same inside my function. But speed is also highly important for my application so if speed might be compromised I would still rather go for template specialization although that will be less readable/maintainable in my specific case. (I could make the template specialization more readable as well, but it's a big code (not fully written by me) and I rather not go through the whole code and change many things to make it nice again. Yes basically I'm lazy.)

(I think the question can go without MWE but if not I'll add some)

C. Binair
  • 419
  • 4
  • 14
  • Compiler are good to propagate constant, so `if (std::is_same::value)` would probably result in no branches with optimization turn on. – Jarod42 Mar 09 '21 at 11:14
  • 1
    C++17 has `if constexpr` to "discard" wrong branch (which allows to write with "incompatible types"). – Jarod42 Mar 09 '21 at 11:16
  • @Jarod42 for your first comment. The function could be called with different `T` but the order will always be the same so first with `T=T1`, then `T=T2`, .. , `T=T1`, `T=T2`,... Is you comment then still valid? – C. Binair Mar 09 '21 at 11:25
  • @Jarod42 for your second comment. I'm aware of `if constexpr`. Unfortunately my boss doesn't want to move from C++11 at the moment... – C. Binair Mar 09 '21 at 11:26
  • 3
    `foo` and `foo` are **different** functions with "different" generated code. so for `foo`, it would be as-if you write `if (true) {..}` and compiler might optimize-out the condition and the `else` branch. for `foo`, it would result in `if (false) {..}` and now, condition and true branch would be optimized-out. – Jarod42 Mar 09 '21 at 11:32
  • 1
    As @Jarod42 says, compilers don't need `if constexpr` to optimize away a condition that they can prove is compile-time-constant, and do dead-code-removal for the not-taken side. (Unlike gcc/clang, MSVC in debug mode will generate both sides and make asm that stupidly tests a constant against another constant instead of using an unconditional branch, but even MSVC gets this right in release mode.) Try it on https://godbolt.org/ and look at the generated asm. But TL:DR: you're fine unless you care about Debug-mode performance on MSVC. – Peter Cordes Mar 09 '21 at 13:56

0 Answers0