News for hackers. Stuff that matters.
Re:Branch predictor: How many “if”s are too many? by flakiness on Thursday May 06, 2021 @05:19PM (#27069182)
In old days people used #ifdef to compile things out so that the "production code" doesn't have any unnecessary branches. I was shocked when I first saw these living if()s in the server-side C++ code but then realized it was vital for debugging in production.

People also used to prefer table based jump over long if-else-if chain for anecdotal performance reasons. That has gradually changed over the evolution of CPUs with various optimizations like the branch prediction. Now some JIT impls like tracing JIT replace virtual function calls with if-elses.

By the way, I'm glad that the article is so thoughtfully written that it doesn't include any ambiguous "best practices" at the end. It could have created yet another myth. The three top tips in the article is more about stating facts. I admire the author taking that stance.

Re:Branch predictor: How many “if”s are too many? by Retric on Thursday May 06, 2021 @10:37PM (#27071376)
Jump tables can still be vastly faster than if-else-if though it really depends on the specifics such as the frequency of each option being correct. Best practices are always subservient to benchmarks when optimizing.
Re:Branch predictor: How many “if”s are too many? by samus on Friday May 07, 2021 @06:43AM (#27074455)
The greatest advantage of jump tables is that they are flexible, within certain margins. You could populate them with entries from a config file or a database without having to go through a full release cycle.
Re:Branch predictor: How many “if”s are too many? by asveikau on Thursday May 06, 2021 @11:56PM (#27071960)
> Now some JIT impls like tracing JIT replace virtual function calls with if-elses.

I think it was about 2010ish when I first noticed a C compiler (Microsoft's) doing this optimization for function pointer calls. Might have been a link time optimization, because caller and callee were in different compilation units.

Re:Branch predictor: How many “if”s are too many? by Too on Sunday May 09, 2021 @02:32AM (#27093746)
I think ifs based on constants get optimized to nothing anyway. So really no need to use #ifdef.
Re:Branch predictor: How many “if”s are too many? by superdimwit on Thursday May 06, 2021 @11:47PM (#27071916)
Big if/else lists will often get compiled into a jump table anyway.