From 09db991e3797f56424ccd205083df79e4965b93a 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. llvm-svn: 232265 --- llvm/include/llvm/ADT/STLExtras.h | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/llvm/include/llvm/ADT/STLExtras.h b/llvm/include/llvm/ADT/STLExtras.h index 57af18e..6b6b0d9 100644 --- a/llvm/include/llvm/ADT/STLExtras.h +++ b/llvm/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.7.4