In my code there's a regular: if a if
statement is true, it will keep true for a while, and if it changes to false, it will keep false for a while. Since the performance in this code matters, I want to make the branch predict more efficient.
Currently what I tried is to write two versions of this if
statement, one is optimized with "likely" and the other is optimized with "unlikely" and use a function pointer to save which one to use, but since function pointer breaks the pipeline either, the benchmark seems no different with normal if
statement. So I'm curious if there's any tech to let CPU "remember" the last choice of this if
statement?
Or, do I really need to care about this?
If it stays the same for a while, the branch predictor will figure that out pretty quickly. That's why sorting an input sometimes makes code run significantly faster; the random unsorted data keeps changing the test result back and forth with no pattern the branch predictor can use, but with sorted data, it has long runs where the branch is always taken or always not taken, and that's the easiest case for branch predictors to handle.
Don't overthink this; let the branch predictor do its job. You don't need to care about it.