diff --git a/llvm/include/llvm/ADT/STLExtras.h b/llvm/include/llvm/ADT/STLExtras.h --- a/llvm/include/llvm/ADT/STLExtras.h +++ b/llvm/include/llvm/ADT/STLExtras.h @@ -1053,6 +1053,23 @@ return array_pod_sort_comparator; } +#ifdef EXPENSIVE_CHECKS +namespace detail { + +inline unsigned presortShuffleEntropy() { + static unsigned Result(std::random_device{}()); + return Result; +} + +template +inline void presortShuffle(IteratorTy Start, IteratorTy End) { + std::mt19937 Generator(presortShuffleEntropy()); + std::shuffle(Start, End, Generator); +} + +} // end namespace detail +#endif + /// array_pod_sort - This sorts an array with the specified start and end /// extent. This is just like std::sort, except that it calls qsort instead of /// using an inlined template. qsort is slightly slower than std::sort, but @@ -1074,8 +1091,7 @@ auto NElts = End - Start; if (NElts <= 1) return; #ifdef EXPENSIVE_CHECKS - std::mt19937 Generator(std::random_device{}()); - std::shuffle(Start, End, Generator); + detail::presortShuffle(Start, End); #endif qsort(&*Start, NElts, sizeof(*Start), get_array_pod_sort_comparator(*Start)); } @@ -1091,8 +1107,7 @@ auto NElts = End - Start; if (NElts <= 1) return; #ifdef EXPENSIVE_CHECKS - std::mt19937 Generator(std::random_device{}()); - std::shuffle(Start, End, Generator); + detail::presortShuffle(Start, End); #endif qsort(&*Start, NElts, sizeof(*Start), reinterpret_cast(Compare)); @@ -1103,8 +1118,7 @@ template inline void sort(IteratorTy Start, IteratorTy End) { #ifdef EXPENSIVE_CHECKS - std::mt19937 Generator(std::random_device{}()); - std::shuffle(Start, End, Generator); + detail::presortShuffle(Start, End); #endif std::sort(Start, End); } @@ -1116,8 +1130,7 @@ template inline void sort(IteratorTy Start, IteratorTy End, Compare Comp) { #ifdef EXPENSIVE_CHECKS - std::mt19937 Generator(std::random_device{}()); - std::shuffle(Start, End, Generator); + detail::presortShuffle(Start, End); #endif std::sort(Start, End, Comp); }