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

ast: Improve set implementation to memoize groudness #2966

Merged
merged 1 commit into from
Dec 3, 2020

Conversation

tsandall
Copy link
Member

@tsandall tsandall commented Dec 2, 2020

See the commit message for details.

Previously sets were not memoizing the groundness check so each call
to `IsGround` would iterate until it found a non-ground term. In the
worst-case, this would check the entire set. Because we call
`IsGround` as part of the inner-loop of the
`topdown.evalTerm#enumerate` code-path for sets, this introduced a
cross-product!

This commit improves the set implementation to maintain a bit that
indicates whether the set is ground. When new elements are added to
the set, the bit is set accordingly.

This change provides a significant speedup for large sets.

name                   old time/op  new time/op  delta
SetIteration/10-20     28.6µs ± 1%  27.3µs ± 1%   -4.65%  (p=0.008 n=5+5)
SetIteration/100-20     263µs ± 1%   152µs ± 1%  -42.20%  (p=0.008 n=5+5)
SetIteration/1000-20   12.5ms ± 0%   1.4ms ± 1%  -88.67%  (p=0.008 n=5+5)
SetIteration/10000-20   1.12s ± 0%   0.02s ± 1%  -98.63%  (p=0.008 n=5+5)

Signed-off-by: Torin Sandall <torinsandall@gmail.com>
Copy link
Contributor

@koponen-styra koponen-styra left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link
Member

@ashutosh-narkar ashutosh-narkar left a comment

Choose a reason for hiding this comment

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

lgtm

Copy link
Contributor

@srenatus srenatus left a comment

Choose a reason for hiding this comment

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

🚀

@tsandall tsandall merged commit 00be8a2 into open-policy-agent:master Dec 3, 2020
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