optimize Expr.equal, cache Expr.simplify #285
Merged
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.
Thanks to hashconsing, we have the properties that
e1 = e2
<=>e1 == e2
, thus we can directly usephys_equal
instead of checking the tag inExpr.equal
.I added a cache mechanism to
Expr.simplify
. Asequal
andhash
are O(1), this is basically free.The goal is for me to be able to call simplify a lot in Owi (even in cases where it may already have been simplified) without being afraid of re-doing too much work. It's a good way to fix the issue I'm having until we encode the simplification status in the type of expressions.
I believe we could internally optimize this further by sharing a cache between
simplify
andsimplify_expr
, but it not as trivial as it seem so I left it out for now.I quickly checked on Owi and did not find any performance regression with this (and also no improvement but that's because I'm not going simplify that much for now, and because I probably don't have a lot of expressions appearing many times).