▲ | marginalia_nu 10 days ago | |||||||
The compiler can't know from the code alone which branch is more likely. This is a property of the input data and not the code. Really advanced JIT compilers can sometimes do those types of optimizations, but this is a fairly rare scenario. | ||||||||
▲ | whitehexagon 10 days ago | parent | next [-] | |||||||
The other day I spotted Zig has @branchHint, not tried it yet, my code isnt that hot! | ||||||||
▲ | jijijijij 10 days ago | parent | prev [-] | |||||||
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. | ||||||||
|