-
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鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
ast/term: sort set's keys slice on insert #4028
ast/term: sort set's keys slice on insert #4028
Conversation
d010b33
to
cab0335
Compare
We cannot get rid of the other sort calls,
because we're returning the underlying keys slice in |
I've looked more into this, but returning a copy of the terms from |
05f392f
to
52befca
Compare
OK, I've dialled this back a little: we now do what's advertised in the title: sort sets at insertion time. We still sort them (again) when comparing, or when printing them, because we don't know the ordering hasn't been messed with. |
52befca
to
4ebe6c7
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.
I think we should just make this PR do the sort-on-insert change to sets. The reason is that the data structures do not support in-place mutation today--if something reaches into the elements and mutates them, we're going to get undefined behaviour with Iter() and other functions. Unfortunately, sort-on-compare does not solve this. Let's yank those other changes out and just sort-on-insert in sets.
d0e90e7
to
da87d15
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, 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>
da87d15
to
a2f93ea
Compare
@@ -1348,15 +1348,12 @@ func (s *set) String() string { | |||
} | |||
var b strings.Builder | |||
b.WriteRune('{') |
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.
@tsandall sorry f-pushed another one after your review -- this was the change I had not included before 馃憞
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 itwas 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 elementinto its sorted position.
Note that we still sort in-place on Compare, because its possible the
sets values have been altered in different ways (e.g. test TestSetCopy).
Analogous to #3823, where we did this with objects'
keys
slices.Also noticed a problem of #3823: the ordering could me messed up in ways different from inserting key/val pairs, e.g. by changing key names in Iter(), so we'll need to care about our invariant after the Iter() call.
馃毀 Some discussion items below 馃憞