SSoelvsten / adiar

An I/O-efficient implementation of (Binary) Decision Diagrams
https://ssoelvsten.github.io/adiar/
MIT License
24 stars 13 forks source link

Use logical rather than boolean operators in comparators #256

Closed SSoelvsten closed 2 years ago

SSoelvsten commented 2 years ago

The || and && operators have shortcutting behaviour. This is good when we need to test for unsafe behaviour, but otherwise these introduce conditional jumps statements which modern CPUs do not work well with. On the other hand, the logical operators | and & have no shortcutting behaviour; hence they use no jumps and use half the number of instructions (see here for some more discussion on the subject).

In C++ any bool value is either 0 or 1 (and nothing else). This also includes the result of == and < etc which can be done in a single instruction each. That means in many places we can use the logical operators | and & instead and not change the computation result! Yet, this is not a very common thing to do, so every time we do so, we need to add a little comment with an explanatory warning.

We only want to do it in the following places:

SSoelvsten commented 2 years ago

If #162 is not yet done, then one may also do the same for the many predicates in adiar/data.h

SSoelvsten commented 2 years ago

@AnnaBlume99 Based on our discussion and the improvements above, I hope you have a better second attempt at this? :)

SSoelvsten commented 2 years ago

Our initial experiments in #271 make it seem like a bad idea.