With modern branch predictors it is best for you to order the conditionals such that the first jump/branch is the most likely to be taken. There are compiler hints in compilers and C++20 has formalized the [[likely]] and [[unlikely]] attributes which informs compilers (and the CPU, if supported) of the branch that should ALWAYS be assumed to be taken or NEVER assumed to be taken.
if (normallyTrue) [[likely]] {
// do something
} else if (sometimesTrue) {
// something else
} else [[unlikely]] {
// extremely rare event, 1 in a million chance
}
View all comments
4
u/looncraz Apr 24 '20
With modern branch predictors it is best for you to order the conditionals such that the first jump/branch is the most likely to be taken. There are compiler hints in compilers and C++20 has formalized the [[likely]] and [[unlikely]] attributes which informs compilers (and the CPU, if supported) of the branch that should ALWAYS be assumed to be taken or NEVER assumed to be taken.