-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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/term: don't sort an object's keys slice in-place #3823
Conversation
Signed-off-by: Stephan Renatus <stephan.renatus@gmail.com>
This is an alternative to the previous commit: instead of sorting on each call including Compare() or String(), we'll sort the key slice on insertion of a new value. Signed-off-by: Stephan Renatus <stephan.renatus@gmail.com>
Signed-off-by: Stephan Renatus <stephan.renatus@gmail.com>
"foo": "bar", | ||
"fizz": "buzz", | ||
} | ||
globals = {"fizz": "buzz", "foo": "bar"} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm not sure what to make of this one. I hadn't expected changes to how source files are formatted coming from this 🤔
Signed-off-by: Stephan Renatus <stephan.renatus@gmail.com>
As observed in the original issue, the failure does not happen on every run. To observe the added test case _failing_ on branch main, use a test call like go test -v ./topdown -run TestRego/partialsetdoc:_object_sort_while_iter -count=10 -v The added `sort_bindings: true` is not required for the topdown eval, but when executing this on the wasm engine, the (unspecified, non- guaranteed) order is reversed. Signed-off-by: Stephan Renatus <stephan.renatus@gmail.com>
Running some benchmarks with this: As is to be expected,
This is "paid for" by extra work during object creation, like the added benchmark shows:
Note that the "increasing keys" object created in the benchmark ( |
Signed-off-by: Stephan Renatus <stephan.renatus@gmail.com>
767f1bb
to
dc6ec5e
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Before, it depended on the elements being passed in ordered by their rows. Before open-policy-agent#3823, the iteration order was the same as the row order; but with sorting the keys slice (which determines iteration order) on creation, that was changed. Now, we'll sort the elements within `groupIterable`. Fixes open-policy-agent#3849. Signed-off-by: Stephan Renatus <stephan.renatus@gmail.com>
Before, it depended on the elements being passed in ordered by their rows. Before #3823, the iteration order was the same as the row order; but with sorting the keys slice (which determines iteration order) on creation, that was changed. Now, we'll sort the elements within `groupIterable`. Fixes #3849. Signed-off-by: Stephan Renatus <stephan.renatus@gmail.com>
Before, it depended on the elements being passed in ordered by their rows. Before open-policy-agent#3823, the iteration order was the same as the row order; but with sorting the keys slice (which determines iteration order) on creation, that was changed. Now, we'll sort the elements within `groupIterable`. Fixes open-policy-agent#3849. Signed-off-by: Stephan Renatus <stephan.renatus@gmail.com>
* format: make groupIterable sort by row Before, it depended on the elements being passed in ordered by their rows. Before #3823, the iteration order was the same as the row order; but with sorting the keys slice (which determines iteration order) on creation, that was changed. Now, we'll sort the elements within `groupIterable`. Fixes #3849. Also includes: * ast/term_bench_test: fix benchmark Since map iteration is randomized in golang, this benchmark didn't actually measure what was intended, but rather the presence or ab- sence of duplicate keys. Now, we'll create a set of keys to insert before, and either shuffle it or use it in its increasing order: to call `(Object).Insert()` in a loop. * CHANGELOG/capabilities: update for v0.33.1 bugfix release Signed-off-by: Stephan Renatus <stephan.renatus@gmail.com>
…nt#3823) * ast/term: sort-on-insert for object.keys slice Instead of sorting on each call including Compare() or String(), we'll sort the key slice on insertion of a new value. * testcases: add case As observed in the original issue, the failure does not happen on every run. To observe the added test case _failing_ on branch main, use a test call like go test -v ./topdown -run TestRego/partialsetdoc:_object_sort_while_iter -count=10 -v The added `sort_bindings: true` is not required for the topdown eval, but when executing this on the wasm engine, the (unspecified, non- guaranteed) order is reversed. Fixes open-policy-agent#3819 Signed-off-by: Stephan Renatus <stephan.renatus@gmail.com> Signed-off-by: Dolev Farhi <farhi.dolev@gmail.com>
* format: make groupIterable sort by row Before, it depended on the elements being passed in ordered by their rows. Before open-policy-agent#3823, the iteration order was the same as the row order; but with sorting the keys slice (which determines iteration order) on creation, that was changed. Now, we'll sort the elements within `groupIterable`. Fixes open-policy-agent#3849. Also includes: * ast/term_bench_test: fix benchmark Since map iteration is randomized in golang, this benchmark didn't actually measure what was intended, but rather the presence or ab- sence of duplicate keys. Now, we'll create a set of keys to insert before, and either shuffle it or use it in its increasing order: to call `(Object).Insert()` in a loop. * CHANGELOG/capabilities: update for v0.33.1 bugfix release Signed-off-by: Stephan Renatus <stephan.renatus@gmail.com> Signed-off-by: Dolev Farhi <farhi.dolev@gmail.com>
Before, we'd sort a set's `keys` slice in-place, and only when it was actually compared to another set. This side-effect of a comparison can be unexpected. Now, we'll keep the `keys` slice sorted by inserting every new element into its sorted position. Analogous to open-policy-agent#3823, where we did this with objects' `keys` slices. Signed-off-by: Stephan Renatus <stephan.renatus@gmail.com>
Before, we'd sort a set's `keys` slice in-place, and only when it was actually compared to another set. This side-effect of a comparison can be unexpected. Now, we'll keep the `keys` slice sorted by inserting every new element into its sorted position. Analogous to #3823, where we did this with objects' `keys` slices. Signed-off-by: Stephan Renatus <stephan.renatus@gmail.com>
Before, we'd sort a set's `keys` slice in-place, and only when it was actually compared to another set. This side-effect of a comparison can be unexpected. Now, we'll keep the `keys` slice sorted by inserting every new element into its sorted position. Analogous to open-policy-agent#3823, where we did this with objects' `keys` slices. Signed-off-by: Stephan Renatus <stephan.renatus@gmail.com>
Two different commits, two different approaches to fix the underlying problem that during evaluation, we may sort an object's key slice; and that could happen within the function passed to the object's
Iter()
method, causing the weird situation that on subsequent invocations of the passed function, the same key would be presented.This example shows how it's possible, and I believe that this is what happened in #3819.
The partial set memo work that seemed to be the cause of the problem must have changed one thing: before, the document under
data.external["test.target"]
would not have been Compare'd to anything, and afterwards, it would. (I haven't uncovered the complete code flow yet...)This was introduced in 2381824, and there's a TODO comment hinting at this being not ideal. I suppose we want to contemplate which way to go here (squash and merge, or drop the second commit doing the sort at insertion-time).
Fixes #3819.