Those familiar with branchless techniques know that, in some cases, they can deliver better performance than non-branchless techniques, but why does it seem that this isn’t always the case? Even in the very same examples? The answer is simple: it all depends on whether the CPU can predict the jumps. If it can predict them – as a repetitive action – it can cache the pattern and determine whether an ‘if’ statement will be executed or not, by assuming its location in memory at that moment, or something similar. However, the predictor often fails significantly with events related to user input, as it cannot predict whether or not a key will be pressed; consequently, it fails and performance drops. However, this usually only occurs at the start (in the case of input, for example, if you repeatedly access the same location in memory, the predictor – whilst not predicting exactly – does come close).
Is it necessary to take this into account in compilers such as GCC? Probably not; the optimisations carried out by modern GCC are generally sufficient.
I’m no expert in C++, but that was my experience...
When to use branchless techniques, and when not to
When to use branchless techniques, and when not to
Irrlicht is love, Irrlicht is life, long live to Irrlicht
Re: When to use branchless techniques, and when not to
This is really a processor thing. Failed predictions can reset the command pipeline - the processor has to throw out a lot of commands and start filling it fresh. And it obviously can happen at every branch or you wouldn't need the branches. You can give hints about the "usual" branch to the compiler. I think otherwise it'll likely assume an if will be true by default. Not really much more you can do.
But processor pipelines can be complicated as hell these days - really hard to predict what it'll do. And my guess would be that compilers already try to make your code as branchless as possible anyway.
But processor pipelines can be complicated as hell these days - really hard to predict what it'll do. And my guess would be that compilers already try to make your code as branchless as possible anyway.
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm