Repository navigation
Add eager bool functions: Bool.(logand, logor, logxor). - #13662
Conversation
nojb
left a comment
There was a problem hiding this comment.
LGTM, modulo suggestion below.
A second approval is needed before the PR can be merged.
lthls
left a comment
There was a problem hiding this comment.
I tried with 5.2.0, forcing inlining of the is_valid functions in the benchmark, and I get improbable results (the eager version is 15 to 20 times faster). It turns out that the whole loop body is removed; I suspect a bug somewhere in the native backend (the backend probably makes some reasonable assumptions that this PR breaks).
Please don't merge until I understand what's going on. Otherwise, I'm in favour of the PR.
Sorry for the noise; it turns out that the eager version can figure out that the counter isn't used and doesn't compute it at all. I suppose the branches in the lazy version prevent this optimisation from applying there. |
dra27
left a comment
There was a problem hiding this comment.
Is it worth being explicit that logand evaluates b2 even if b1 evaluates to false and likewise for logor?
My feeling is that the extra explanation may be more confusing than clarifying to the casual reader, as that is the normal behaviour of functions; so personally I would leave it as it is. Except this minor point, the PR now has three official approvals, hence it is good to go! |
Motivation
This PR is based on a request from Oleg Kiselyov, who mentioned recently that it would be useful to have eager logical boolean operations as well as the short-circuiting (lazy) versions. The lazy versions generate code with conditional jumps that might sometimes be slower than a sequence of bitwise operations.
Implementation
Branchless implementations of the operators are easy, since we can just reuse the existing primitives for int:
Benchmarking
The eager operators are sometimes faster than the lazy operators. Here's a benchmark that tests eager and lazy versions of
Uchar.is_validon an array of integers, half of which are valid. On my system, with OCaml 5.2.0, the results are as follows:Other implementations
There's an implementation in Base (here) and an unoptimized implementation in Batteries with funny names (
logandis calledmulandlogoris calledadd).