Consider the following code. The bool predicate
is either true or false depends on the user. So we can’t tell which one is more likely.
if (predicate)
logicA()
else
logicB()
However, there is an advantage that, once predicate
is set, it is guaranteed not to be modified any more.
My question is:
- Will this case here actually introduce branch miss penalty? I have done some searching, and I understand the branch predictor in modern CPUs are quite smart. For example, they can dynamicly predict. So in my case, it could be that the predictor found the predicate is always true, so it will then switch to always predict
logicA()
. - If the branch miss is not evictable, is there any way to optimize my problem? To my knowledge, Linux can rewrite its codes to switch from “debug” mode and “no-debug” mode. This strategy certainly won’t apply to my case, because it is hard to define the “start time” itself. When should the compiler or CPU start to presume
predicate
is initialized and will not change anymore? I think it is a tough task for them. So I wonder if you could come up with some brilliant ideas?
13
If your predicate switches from a run a falses to a run of trues, then the branch predictor should have some misses the first handful of trues, then switch to an accurate prediction thereafter.
If this turns out to be too expensive, then you can use a function pointer as the thing you call, and reassign it once when the user does the thing. This might affect other optimizations though (such as inlining), so it’s really anyone’s guess.
I’d measure on target architectures.
8