Consider the follow snippet which steadily happens in tests:
std::vector<std::pair<int, int>> v(SomeData()); std::sort(v.begin(), v.end(), [](const auto& lhs, const auto& rhs) { return lhs.first < rhs.first; });
Under this assumption all elements in the vector whose first elements are equal do not guarantee any order. Unfortunately, this prevents libcxx introducing other implementations because tests might silently fail and the users might heavily depend on the stability of implementations.
Provide functionality for randomizing the unspecified behavior so that the users can test and migrate their components and libcxx can introduce new sorting algorithms and optimizations to the containers.
For example, as of LLVM version 13, libcxx sorting algorithm takes O(n^2) worst case but according to the standard its worst case should be O(n log n). This effort helps users to gradually fix their tests while updating to new faster algorithms.
_LIBCPP_DEBUG_RANDOMIZE_UNSPECIFIED_STABILITY which should be a part of the libcxx config.std::shuffle availability.std::random_device for seeding the random number generator. This guarantees the same stability guarantee within a run but not through different runs, for example, for tests become flaky and eventually be seen as broken. For platforms which do not support ASLR, the seed is fixed during build._LIBCPP_RANDOMIZE_UNSPECIFIED_STABILITY_SEED=seed definition.This comes with some side effects if any of the flags is on:
Google has measured couple of thousands of tests to be dependent on the stability of sorting and selection algorithms. As we also plan on updating (or least, providing under flag more) sorting algorithms, this effort helps doing it gradually and sustainably. This is also bad for users to depend on the unspecified behavior in their tests, this effort helps to turn this flag in debug mode.
None if the flag is off. If the flag is on, it may lead to some non-reproducible results, for example, for caching.
std::sort, there is no guarantee on the order of equal elementsstd::partial_sort, there is no guarantee on the order of equal elements and : on the order of the remaining partstd::nth_element, there is no guarantee on the order from both sides of the : partitionPatches welcome.