Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use logical rather than boolean operators in comparators #256

Closed
2 tasks
SSoelvsten opened this issue Jan 28, 2022 · 3 comments
Closed
2 tasks

Use logical rather than boolean operators in comparators #256

SSoelvsten opened this issue Jan 28, 2022 · 3 comments
Assignees
Labels
good first issue Good for newcomers ✨ optimisation It's all about speed / space

Comments

@SSoelvsten
Copy link
Owner

SSoelvsten commented Jan 28, 2022

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:

  • Our comparators run a linearithmic number of times with a small likelihood they are predictable.
  • Our predicates in internal/data.cpp on binary operators (e.g. is_left_shortcutting).
@SSoelvsten
Copy link
Owner Author

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

@SSoelvsten
Copy link
Owner Author

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

@SSoelvsten
Copy link
Owner Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers ✨ optimisation It's all about speed / space
Projects
None yet
2 participants