Skip to content

Commit bfebc63

Browse files
committed
[ADT][Expensive checks] Create a std::random_device seed only once when shuffling before sorting
This speeds up the build of compiler-rt with an expensive checks enabled clang by an order of 1 or 2 magnitudes on my machine. I was hoping this would also fix the 'large.test' libFuzzer timeout on the expensive checks bot on green dragon http://lab.llvm.org:8080/green/job/clang-stage1-cmake-RA-expensive/, but the fuzzer test still takes too long to compile because of other IR/MIR verification inefficiencies. Differential Revision: https://reviews.llvm.org/D70288
1 parent 77f8a33 commit bfebc63

File tree

1 file changed

+21
-8
lines changed

1 file changed

+21
-8
lines changed

llvm/include/llvm/ADT/STLExtras.h

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1053,6 +1053,23 @@ inline int (*get_array_pod_sort_comparator(const T &))
10531053
return array_pod_sort_comparator<T>;
10541054
}
10551055

1056+
#ifdef EXPENSIVE_CHECKS
1057+
namespace detail {
1058+
1059+
inline unsigned presortShuffleEntropy() {
1060+
static unsigned Result(std::random_device{}());
1061+
return Result;
1062+
}
1063+
1064+
template <class IteratorTy>
1065+
inline void presortShuffle(IteratorTy Start, IteratorTy End) {
1066+
std::mt19937 Generator(presortShuffleEntropy());
1067+
std::shuffle(Start, End, Generator);
1068+
}
1069+
1070+
} // end namespace detail
1071+
#endif
1072+
10561073
/// array_pod_sort - This sorts an array with the specified start and end
10571074
/// extent. This is just like std::sort, except that it calls qsort instead of
10581075
/// using an inlined template. qsort is slightly slower than std::sort, but
@@ -1074,8 +1091,7 @@ inline void array_pod_sort(IteratorTy Start, IteratorTy End) {
10741091
auto NElts = End - Start;
10751092
if (NElts <= 1) return;
10761093
#ifdef EXPENSIVE_CHECKS
1077-
std::mt19937 Generator(std::random_device{}());
1078-
std::shuffle(Start, End, Generator);
1094+
detail::presortShuffle<IteratorTy>(Start, End);
10791095
#endif
10801096
qsort(&*Start, NElts, sizeof(*Start), get_array_pod_sort_comparator(*Start));
10811097
}
@@ -1091,8 +1107,7 @@ inline void array_pod_sort(
10911107
auto NElts = End - Start;
10921108
if (NElts <= 1) return;
10931109
#ifdef EXPENSIVE_CHECKS
1094-
std::mt19937 Generator(std::random_device{}());
1095-
std::shuffle(Start, End, Generator);
1110+
detail::presortShuffle<IteratorTy>(Start, End);
10961111
#endif
10971112
qsort(&*Start, NElts, sizeof(*Start),
10981113
reinterpret_cast<int (*)(const void *, const void *)>(Compare));
@@ -1103,8 +1118,7 @@ inline void array_pod_sort(
11031118
template <typename IteratorTy>
11041119
inline void sort(IteratorTy Start, IteratorTy End) {
11051120
#ifdef EXPENSIVE_CHECKS
1106-
std::mt19937 Generator(std::random_device{}());
1107-
std::shuffle(Start, End, Generator);
1121+
detail::presortShuffle<IteratorTy>(Start, End);
11081122
#endif
11091123
std::sort(Start, End);
11101124
}
@@ -1116,8 +1130,7 @@ template <typename Container> inline void sort(Container &&C) {
11161130
template <typename IteratorTy, typename Compare>
11171131
inline void sort(IteratorTy Start, IteratorTy End, Compare Comp) {
11181132
#ifdef EXPENSIVE_CHECKS
1119-
std::mt19937 Generator(std::random_device{}());
1120-
std::shuffle(Start, End, Generator);
1133+
detail::presortShuffle<IteratorTy>(Start, End);
11211134
#endif
11221135
std::sort(Start, End, Comp);
11231136
}

0 commit comments

Comments
 (0)