More efficient info bucket management

Logged at every state_read() and state_write()
This commit is contained in:
Andrea Mazzoleni 2026-02-08 22:56:22 +01:00
parent 83b1c9bf3b
commit d5b5a9bcbf
13 changed files with 375 additions and 495 deletions

View file

@ -854,99 +854,3 @@ unsigned muldiv_upper(uint64_t v, uint64_t mul, uint64_t div)
return (uint32_t)((v * mul + div - 1) / div);
}
/****************************************************************************/
/* sort */
/**
* Three-way partition for quicksort
* Partitions array into three parts: < pivot, == pivot, > pivot
* Returns the boundaries of the equal section via lt and gt pointers
*/
static void partition3way(time_t *arr, size_t left, size_t right, size_t *lt, size_t *gt)
{
/* choose median-of-three as pivot to avoid worst case on sorted data */
size_t mid = left + (right - left) / 2;
time_t pivot;
/* sort left, mid, right and use median as pivot */
if (arr[left] > arr[mid]) {
uint64_t tmp = arr[left]; arr[left] = arr[mid]; arr[mid] = tmp;
}
if (arr[mid] > arr[right]) {
uint64_t tmp = arr[mid]; arr[mid] = arr[right]; arr[right] = tmp;
if (arr[left] > arr[mid]) {
tmp = arr[left]; arr[left] = arr[mid]; arr[mid] = tmp;
}
}
pivot = arr[mid];
// Dutch National Flag partition
size_t i = left;
size_t lt_idx = left;
size_t gt_idx = right;
while (i <= gt_idx) {
if (arr[i] < pivot) {
uint64_t tmp = arr[lt_idx];
arr[lt_idx] = arr[i];
arr[i] = tmp;
lt_idx++;
i++;
} else if (arr[i] > pivot) {
uint64_t tmp = arr[i];
arr[i] = arr[gt_idx];
arr[gt_idx] = tmp;
gt_idx--;
} else {
i++;
}
}
*lt = lt_idx;
*gt = gt_idx;
}
/**
* Recursive three-way quicksort
*/
static void quicksort3way_recursive(time_t *arr, size_t left, size_t right)
{
if (left >= right)
return;
/* use insertion sort for small subarrays (optimization) */
if (right - left < 16) {
for (size_t i = left + 1; i <= right; i++) {
time_t key = arr[i];
size_t j = i;
while (j > left && arr[j - 1] > key) {
arr[j] = arr[j - 1];
j--;
}
arr[j] = key;
}
return;
}
size_t lt, gt;
partition3way(arr, left, right, &lt, &gt);
/*
* Recursively sort elements less than and greater than pivot
* Elements equal to pivot are already in correct position
*/
if (lt > left) {
quicksort3way_recursive(arr, left, lt - 1);
}
if (gt < right) {
quicksort3way_recursive(arr, gt + 1, right);
}
}
void sort_time(time_t *arr, size_t n)
{
if (n <= 1)
return;
quicksort3way_recursive(arr, 0, n - 1);
}