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+topdown: cache array groundness #4151

Merged

Conversation

srenatus
Copy link
Contributor

I had been believing that we couldn't do this, since many methods allow changing an array's elements in ways that would invalidate the cached groundness bit. However, if we don't do that, we're OK.

To get the tests to pass, the walk builtin implementation had implicitly depended on array plugging returning a copy; that's now made explicit.

Fixes #3679.

Comment on lines +44 to +46
module := `package test
fixture := data.fixture
main { x := fixture }`
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I had to make this benchmark oddly specific to see an effect:

name                    old time/op    new time/op    delta
ArrayPlugging/10-16       14.4µs ± 1%    14.3µs ± 3%     ~     (p=0.690 n=5+5)
ArrayPlugging/100-16      22.7µs ±11%    21.0µs ±14%     ~     (p=0.095 n=5+5)
ArrayPlugging/1000-16     95.4µs ± 8%    77.8µs ± 5%  -18.41%  (p=0.008 n=5+5)
ArrayPlugging/10000-16     852µs ± 4%     685µs ± 2%  -19.55%  (p=0.008 n=5+5)

name                    old alloc/op   new alloc/op   delta
ArrayPlugging/10-16       9.83kB ± 0%    9.71kB ± 0%   -1.22%  (p=0.008 n=5+5)
ArrayPlugging/100-16      15.1kB ± 0%    14.1kB ± 0%   -6.22%  (p=0.008 n=5+5)
ArrayPlugging/1000-16     65.7kB ± 0%    57.5kB ± 0%  -12.54%  (p=0.008 n=5+5)
ArrayPlugging/10000-16     574kB ± 0%     492kB ± 0%  -14.30%  (p=0.008 n=5+5)

name                    old allocs/op  new allocs/op  delta
ArrayPlugging/10-16          191 ± 0%       188 ± 0%   -1.57%  (p=0.008 n=5+5)
ArrayPlugging/100-16         371 ± 0%       368 ± 0%   -0.81%  (p=0.008 n=5+5)
ArrayPlugging/1000-16      2.17k ± 0%     2.17k ± 0%   -0.14%  (p=0.008 n=5+5)
ArrayPlugging/10000-16     20.2k ± 0%     20.2k ± 0%   -0.01%  (p=0.008 n=5+5)

@@ -104,6 +107,9 @@ func (u *bindings) plugNamespaced(a *ast.Term, caller *bindings) *ast.Term {
})
return &cpy
case ast.Set:
Copy link
Contributor Author

Choose a reason for hiding this comment

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

For the other three shortcuts added here, I have not yet thought about when they'd happen -- they do seem to make sense in a pattern-match kind of way 🙃

@srenatus srenatus force-pushed the sr/ast/cache-array-groundness branch from bcc3f0e to 5bf956c Compare December 16, 2021 13:51
tsandall
tsandall previously approved these changes Dec 16, 2021
Copy link
Member

@tsandall tsandall left a comment

Choose a reason for hiding this comment

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

LGTM. The TODOs on the bindings look fine to me.

@srenatus
Copy link
Contributor Author

OK great. I'll update this and get it merged.

I had been believing that we couldn't do this, since many methods
allow changing an array's elements in ways that would invalidate
the cached groundness bit. However, if we don't do that, we're OK.

To get the tests to pass, the walk builtin implementation had
implicitly depended on array plugging returning a copy; that's now
made explicit.

In topdown/bindings, this adds a few more shortcuts where applicable
(array/set). Previously, only objects had a ground?- shortcutin
plugging and namespacing vars.

Fixes open-policy-agent#3679.

Signed-off-by: Stephan Renatus <stephan.renatus@gmail.com>
@srenatus srenatus force-pushed the sr/ast/cache-array-groundness branch from 5bf956c to 1842468 Compare December 17, 2021 06:40
@srenatus srenatus marked this pull request as ready for review December 17, 2021 06:40
@srenatus srenatus merged commit d077725 into open-policy-agent:main Dec 17, 2021
@srenatus srenatus deleted the sr/ast/cache-array-groundness branch December 17, 2021 07:05
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.

ast.Array should cache groundness bit to avoid unnecessary computation
2 participants