Skip to content

Commit acae7cf

Browse files
committed
Added early exit conditions
1 parent f49088f commit acae7cf

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

src/xss-common-keyvaluesort.hpp

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ X86_SIMD_SORT_INLINE arrsize_t kvpartition(type_t1 *keys,
7272
for (int32_t i = (right - left) % vtype1::numlanes; i > 0; --i) {
7373
*smallest = std::min(*smallest, keys[left]);
7474
*biggest = std::max(*biggest, keys[left]);
75-
if (keys[left] > pivot) {
75+
if (keys[left] >= pivot) {
7676
right--;
7777
std::swap(keys[left], keys[right]);
7878
std::swap(indexes[left], indexes[right]);
@@ -204,12 +204,13 @@ X86_SIMD_SORT_INLINE arrsize_t kvpartition_unrolled(type_t1 *keys,
204204
return kvpartition<vtype1, vtype2>(
205205
keys, indexes, left, right, pivot, smallest, biggest);
206206
}
207+
207208
/* make array length divisible by vtype1::numlanes , shortening the array */
208209
for (int32_t i = ((right - left) % (num_unroll * vtype1::numlanes)); i > 0;
209210
--i) {
210211
*smallest = std::min(*smallest, keys[left]);
211212
*biggest = std::max(*biggest, keys[left]);
212-
if (keys[left] > pivot) {
213+
if (keys[left] >= pivot) {
213214
right--;
214215
std::swap(keys[left], keys[right]);
215216
std::swap(indexes[left], indexes[right]);
@@ -386,21 +387,27 @@ X86_SIMD_SORT_INLINE void kvsort_(type1_t *keys,
386387
* Base case: use bitonic networks to sort arrays <= 128
387388
*/
388389
if (right + 1 - left <= 128) {
389-
390390
kvsort_n<vtype1, vtype2, 128>(
391391
keys + left, indexes + left, (int32_t)(right + 1 - left));
392392
return;
393393
}
394394

395+
// Ascending comparator for this vtype
396+
using comparator = Comparator<vtype1, false>;
395397
type1_t pivot;
396-
auto pivot_result = get_pivot_smart<vtype1, type1_t>(keys, left, right);
398+
auto pivot_result
399+
= get_pivot_smart<vtype1, comparator, type1_t>(keys, left, right);
397400
pivot = pivot_result.pivot;
398401

402+
if (pivot_result.result == pivot_result_t::Sorted) { return; }
403+
399404
type1_t smallest = vtype1::type_max();
400405
type1_t biggest = vtype1::type_min();
401406
arrsize_t pivot_index = kvpartition_unrolled<vtype1, vtype2, 4>(
402407
keys, indexes, left, right + 1, pivot, &smallest, &biggest);
403408

409+
if (pivot_result.result == pivot_result_t::Only2Values) { return; }
410+
404411
#ifdef XSS_COMPILE_OPENMP
405412
if (pivot != smallest) {
406413
bool parallel_left = (pivot_index - left) > task_threshold;

0 commit comments

Comments
 (0)