Skip to content

Add eager bool functions: Bool.(logand, logor, logxor). - #13662

Merged
dra27 merged 2 commits into
ocaml:trunkfrom
yallop:eager-booleans
Dec 16, 2024
Merged

dra27 merged 2 commits into
ocaml:trunkfrom
yallop:eager-booleans

Conversation

@yallop

@yallop yallop commented Dec 7, 2024

Copy link
Copy Markdown
Member

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:

external logand : bool -> bool -> bool = "%andint"
external logor : bool -> bool -> bool = "%orint"
external logxor : bool -> bool -> bool = "%xorint"

Benchmarking

The eager operators are sometimes faster than the lazy operators. Here's a benchmark that tests eager and lazy versions of Uchar.is_valid on an array of integers, half of which are valid. On my system, with OCaml 5.2.0, the results are as follows:

Name Time/run Percentage
loop_lazy 67.46us 100.00%
loop_eager 41.88us 62.08%

Other implementations

There's an implementation in Base (here) and an unoptimized implementation in Batteries with funny names (logand is called mul and logor is called add).

@nojb nojb left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, modulo suggestion below.

A second approval is needed before the PR can be merged.

Comment thread stdlib/bool.mli Resolved
Comment thread Changes Resolved

@lthls lthls left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@lthls

lthls commented Dec 7, 2024

Copy link
Copy Markdown
Contributor

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).

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.
With a well-placed Sys.opaque_identity, I get reasonable numbers again.

@dra27 dra27 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it worth being explicit that logand evaluates b2 even if b1 evaluates to false and likewise for logor?

@nojb

nojb commented Dec 11, 2024

Copy link
Copy Markdown
Contributor

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!

@dra27
dra27 merged commit c796b3b into ocaml:trunk Dec 16, 2024
@yallop
yallop deleted the eager-booleans branch December 16, 2024 23:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants