-
Notifications
You must be signed in to change notification settings - Fork 17.9k
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
proposal: slices, sort: randomize sort order of equal elements in tests #70182
Comments
Related Issues and Documentation (Emoji vote if this was helpful or unhelpful; more detailed feedback welcome in this discussion.) |
I assume that this would be implemented with the use of |
@mateusz834, I left out any implementation details, but I would be very surprised if this can't be implemented in a way that doesn't impact performance in production. If no such mechanism is available, the slowdown should be a trivially predictable branch. |
What changed since the last proposal (#13884):
|
I don't mean production environments, but benchmarks in the |
Sorry for the misunderstanding. You are correct of course. I am sure there are cases where it matters. The pervious proposal suggested an opt-out mechanism for tests that should also work for benchmarks. |
Proposal Details
Go's (non-stable) sort functions don't guarantee a particular ordering for equal elements. However, since the order of equal elements is always the same for a number of releases, tests tend to accumulate dependencies on the ordering of equal elements by accident.
In Google codebase we noticed that a large number of tests depended on the order of equal elements when the sorting algorithm was changed to pdqsort in April 2022 (http://go.dev/cl/371574). We needed to fix all of these to be able to release pdqsort internally. Recently http://go.dev/cl/624295 changed how equal elements are ordered. In the 2½ years since pdqsort was introduced a few tests have already started to depend on the order of equal elements again.
This is a classic example of Hyrum's Law
Fixing tests when the order of equal elements changes is a lot of work and makes upgrading to a new Go version harder. It's also surprisingly hard to understand if the order of equal elements actually matters (i.e. if stable sort should have been used) or if the order of equal elements was depended upon accidentally. Both problems can be mitigated sufficiently by randomizing the order of equal elements on each test run. That way, tests that depend on the order of equal elements provide a very early signal of problem.
Google already did this for C++ some time ago and it's working very well:
The text was updated successfully, but these errors were encountered: