From c0cc039a579d1691030af9cb536c779c717f5800 Mon Sep 17 00:00:00 2001 From: Benjamin Kramer Date: Sat, 14 Mar 2015 14:53:14 +0000 Subject: [PATCH] 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 --- include/llvm/ADT/STLExtras.h | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/include/llvm/ADT/STLExtras.h b/include/llvm/ADT/STLExtras.h index 57af18e303b..6b6b0d9ad7e 100644 --- a/include/llvm/ADT/STLExtras.h +++ b/include/llvm/ADT/STLExtras.h @@ -263,10 +263,11 @@ inline int (*get_array_pod_sort_comparator(const T &)) /// default to std::less. template inline void array_pod_sort(IteratorTy Start, IteratorTy End) { - // Don't dereference start iterator of empty sequence. - if (Start == End) return; - qsort(&*Start, End-Start, sizeof(*Start), - get_array_pod_sort_comparator(*Start)); + // Don't inefficiently call qsort with one element or trigger undefined + // behavior with an empty sequence. + auto NElts = End - Start; + if (NElts <= 1) return; + qsort(&*Start, NElts, sizeof(*Start), get_array_pod_sort_comparator(*Start)); } template @@ -275,9 +276,11 @@ inline void array_pod_sort( int (*Compare)( const typename std::iterator_traits::value_type *, const typename std::iterator_traits::value_type *)) { - // Don't dereference start iterator of empty sequence. - if (Start == End) return; - qsort(&*Start, End - Start, sizeof(*Start), + // Don't inefficiently call qsort with one element or trigger undefined + // behavior with an empty sequence. + auto NElts = End - Start; + if (NElts <= 1) return; + qsort(&*Start, NElts, sizeof(*Start), reinterpret_cast(Compare)); } -- 2.34.1