Skip to content

Commit c306ac5

Browse files
authored
Merge pull request #197 from r-devulap/early-exit
Add early exit for argsort if array is already sorted
2 parents 14f504c + fc1ffc8 commit c306ac5

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

src/xss-common-argsort.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -596,7 +596,7 @@ X86_SIMD_SORT_INLINE void xss_argsort(T *arr,
596596
bool hasnan = false,
597597
bool descending = false)
598598
{
599-
/* TODO optimization: on 32-bit, use full_vector for 32-bit dtype */
599+
600600
using vectype = typename std::conditional<sizeof(T) == sizeof(int32_t),
601601
half_vector<T>,
602602
full_vector<T>>::type;
@@ -607,6 +607,7 @@ X86_SIMD_SORT_INLINE void xss_argsort(T *arr,
607607
full_vector<arrsize_t>>::type;
608608

609609
if (arrsize > 1) {
610+
/* simdargsort does not work for float/double arrays with nan */
610611
if constexpr (xss::fp::is_floating_point_v<T>) {
611612
if ((hasnan) && (array_has_nan<vectype>(arr, arrsize))) {
612613
std_argsort_withnan(arr, arg, 0, arrsize);
@@ -618,6 +619,11 @@ X86_SIMD_SORT_INLINE void xss_argsort(T *arr,
618619
}
619620
UNUSED(hasnan);
620621

622+
/* early exit for already sorted arrays: float/double with nan never reach here*/
623+
auto comp = descending ? Comparator<vectype, true>::STDSortComparator
624+
: Comparator<vectype, false>::STDSortComparator;
625+
if (std::is_sorted(arr, arr + arrsize, comp)) { return; }
626+
621627
#ifdef XSS_COMPILE_OPENMP
622628

623629
bool use_parallel = arrsize > 10000;

0 commit comments

Comments
 (0)