array_pod_sort: Since we're checking the length anyways also ignore one-element ranges

Sorting them is obviously a noop and we can skip the libc call. This is
surprisingly common in clang. NFC.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@232265 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Benjamin Kramer
2015-03-14 14:53:14 +00:00
parent 439cc2c5de
commit c0cc039a57

View File

@@ -263,10 +263,11 @@ inline int (*get_array_pod_sort_comparator(const T &))
/// default to std::less. /// default to std::less.
template<class IteratorTy> template<class IteratorTy>
inline void array_pod_sort(IteratorTy Start, IteratorTy End) { inline void array_pod_sort(IteratorTy Start, IteratorTy End) {
// Don't dereference start iterator of empty sequence. // Don't inefficiently call qsort with one element or trigger undefined
if (Start == End) return; // behavior with an empty sequence.
qsort(&*Start, End-Start, sizeof(*Start), auto NElts = End - Start;
get_array_pod_sort_comparator(*Start)); if (NElts <= 1) return;
qsort(&*Start, NElts, sizeof(*Start), get_array_pod_sort_comparator(*Start));
} }
template <class IteratorTy> template <class IteratorTy>
@@ -275,9 +276,11 @@ inline void array_pod_sort(
int (*Compare)( int (*Compare)(
const typename std::iterator_traits<IteratorTy>::value_type *, const typename std::iterator_traits<IteratorTy>::value_type *,
const typename std::iterator_traits<IteratorTy>::value_type *)) { const typename std::iterator_traits<IteratorTy>::value_type *)) {
// Don't dereference start iterator of empty sequence. // Don't inefficiently call qsort with one element or trigger undefined
if (Start == End) return; // behavior with an empty sequence.
qsort(&*Start, End - Start, sizeof(*Start), auto NElts = End - Start;
if (NElts <= 1) return;
qsort(&*Start, NElts, sizeof(*Start),
reinterpret_cast<int (*)(const void *, const void *)>(Compare)); reinterpret_cast<int (*)(const void *, const void *)>(Compare));
} }