allow short-circuiting of boolean expressions #11
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This is a tiny opportunistic optimization -- current grammar parsing (left-recursive) disables any kind of short-circuiting that could happen during evaluation. Switching to right-recursive allows the value of the left-expression to decide whether or not to evaluate the rest of the expression, possibly saving time.
The exact spot that can do the shortcircuit now is:
https://github.com/sysbio-curie/MaBoSS-env-2.0/blob/master/engine/src/BooleanNetwork.h#L1442
(and the appropriate one for
Or
).Impact
before
perf
showed that around 98% of the recursive calls in theAndLogicalExpression::eval
were for the "left" branch, spending around 40% total time in the function.after (~1.7× speedup)
perf
shows around 20% total time spent inAndLogicalExpression::Eval
and the left/right evaluations are kinda more balanced (seems veeeery roughly like half and half, which is good).The speedup is model-dependent, models may behave better or worse depending on how the boolean expressions there are written. But there should never be a slowdown as the original behavior was worst-case.
Considerations
if any of the expressions' evaluations are supposed to have side effects, this likely breaks it. I didn't find such behavior, but better check, right?
Eventually it would be very nice to actually reorder the expressions based on complexity (so that the most probable short-circuiting ones are tried first, saving as much power as possible).