schwab: minor fine-tune so we more easily win over std::sort

This commit is contained in:
Richard Thier 2025-05-09 05:35:42 +02:00
parent 95c759b9e3
commit 266f489002

View File

@ -16,7 +16,7 @@
/** Below this many elements we do insertion sort */
#ifndef SCHWAB_INSERTION_THRESHOLD
#define SCHWAB_INSERTION_THRESHOLD 64
#define SCHWAB_INSERTION_THRESHOLD 128
#endif /* SCHWAB_DELTA_THRESHOLD */
typedef uint32_t sch_rand_state;
@ -50,10 +50,11 @@ static inline void schwab_swap(uint32_t *a, uint32_t *b) {
inline void sch_insertion_sort(uint32_t *arr, int low, int high) {
for(int i = low + 1; i <= high; ++i) {
uint32_t key = arr[i];
int j = i;
/* Move elements of arr[0..i-1] that are greater than key */
/* to one position ahead of their current position */
int j = i;
#pragma GCC unroll 2
while(j > 0 && arr[j - 1] > key) {
arr[j] = arr[j - 1];
--j;
@ -62,6 +63,24 @@ inline void sch_insertion_sort(uint32_t *arr, int low, int high) {
}
}
/** Simple insertion sort for small cases v2 - not necessarily better */
inline void sch_insertion_sort2(uint32_t* arr, int low, int high) {
for(size_t i = low + 1; i <= high; ++i) {
uint32_t key = arr[i];
/* Separate load and compare to expose ILP */
int j = i;
#pragma GCC unroll 2
while(j > 0) {
uint32_t prev = arr[j - 1];
if (prev <= key) break;
arr[j] = prev;
--j;
}
arr[j] = key;
}
}
/**
* 3-way partitioning, in middle all the pivot elements.
*