by mcv · 3y ago · 0 comments
But these kind of tricks feel like we need to con the compiler into optimising this correctly, which is of course ridiculous. What we probably need instead is if-statements that we can tell what's most likely the correct prediction.
Something like:
if v > maxV predict true
maxV = v
continueSo really you end up having to make assumptions about the input to get the performance boost.
The CPU's branch predictor won't be able to perform that kind of algorithmic analysis, but patterns like the above also work reasonably well for simpler heuristics like 'predict the same outcome as the last time the branch was taken'.