▲ | jijijijij 10 days ago | |
Isn't branch prediction mostly a CPU thing? Do you have an example with corresponding assembly? It's not that I don't believe you about the performance impact, as I have observed the same with e.g. Rust in some cases, but I don't think it has a lot to do with the compiler judging what's more likely, but rather more or less "random" optimization differences/bugs. At least in my case, the ordering had nothing to do with likelihood, or even had a reverse correlation. I think in your example a compiler may or may not realize the code is semantically equivalent and all bets are off about what's going to happen optimization-wise. I mean, in the end it doesn't matter for the commenting issue, as you are realistically not going to fix the compiler to have slightly more readable code. | ||
▲ | marginalia_nu 10 days ago | parent [-] | |
I don't have an assembly output for this particular case, but how I understand it is that the re-write basically turns it into two separate conditions, which means the branch predictor is free to model their outcomes separately. In this case, the data is bimodal, depending on the chosen input, two likely outcomes exists. Either no looping is needed, or much looping is needed. This seemingly confuses the branch predictor when it's the same branch dealing with both scenarios. |