asort: replace old quicksort fallback with glibc 2.43 mergesort+heapsort#129
asort: replace old quicksort fallback with glibc 2.43 mergesort+heapsort#129nGoline wants to merge 1 commit into
Conversation
The old fallback (used when the native `qsort_r` lacks the glibc calling convention) was a copy of glibc's 2004 median-of-three quicksort. It had a subtle self-comparison bug: when the left and right scan pointers both converged on the pivot with an all-equal array, the inner loop evaluated `cmp(pivot, pivot)` before detecting `left_ptr == right_ptr` and breaking. Comparators that use pointer identity to detect distinct elements would spuriously fire on this. Replace it with glibc 2.43's stable mergesort (`heapsort` fallback on `malloc` failure). Mergesort always compares from two distinct halves and never produces self-comparisons. Portability adaptations to remove glibc-internal dependencies: - `__compar_d_fn_t` -> typedef from `_total_order_cb` - `__memswap` / `__mempcpy` -> local static inline helpers - `pthread_cleanup_*` -> removed (no thread cancellation needed) - `__set_errno` → `errno = …` - `libc_hidden_def` / `weak_alias` / `__qsort_r` -> removed / renamed to `_asort` Add tests for all-equal and single-element arrays.
|
This one needs to be a PR on ccan. I'm surprised we can't just use a glibc built in for this, seems like a lot of effort just to sort. Would be interesting to see a profiler output for a large node to see if sorting is showing up as a performance issue 🤔 |
It is ;)
We do when the platform has it. The effort here isn't really "writing a sort", it's that the existing fallback was a copy of
Agreed, would be interesting! Worth flagging though that this path only runs on non-GNU platforms (so not your typical Linux node), and the change is a correctness fix rather than a perf one. |
Lol indeed it is 😂😂 |
There was a problem hiding this comment.
Still find it crazy we have to do this but ACK 1628832.
Seems good to have a fallback in general, though. For Mac it seems prudent to figure out how to get the build system to use its BSD qsort_r but it isn't urgent.
I left a tag on some asserts that would help me sleep at night but it appears the code technically shouldn't need them.
|
|
||
| static inline void | ||
| swap_words_64 (void * restrict a, void * restrict b, size_t n) | ||
| { |
There was a problem hiding this comment.
can we add an assert here, something like assert(n && n % 8 == 0)
| static inline void | ||
| swap_words_32 (void * restrict a, void * restrict b, size_t n) | ||
| { | ||
| do |
The bug
The old fallback
_asort(a copy of glibc's 2004 median-of-three quicksort) calledcmp(a, a), the same pointer on both sides, when sorting arrays where all elements compare equal. In the inner loop, after the left and right scan pointers both stop on the pivot element, two comparisons of the formcmp(pivot, pivot)execute before theleft_ptr == right_ptrguard fires and breaks.This violates the implicit contract that comparators can assume
a != bwhen called by a sort routine, which is a reasonable expectation since the C standard says comparator behaviour for equal-valued distinct elements is unspecified but never requires handling identical pointers.The fix
Replace the old quicksort with the algorithm from glibc 2.43: a stable top-down mergesort that uses a stack buffer for small arrays and falls back to in-place heapsort if malloc fails. Mergesort always compares across two distinct sub-array halves, so self-comparisons cannot occur.
Portability
The glibc 2.43 source uses several glibc-internal headers and symbols that are not available outside glibc (
memswap.h,pthreadP.h,__compar_d_fn_t,pthread_cleanup_combined_*, etc.). This PR replaces each with a portable equivalent; the algorithm itself is unchanged.Tests
Added four new test cases:
cmp(&val, &val, NULL)explicitly and asserts it returns 0, pinning the contract that comparators passed toasortmust handle identical pointers gracefully.The
diagline reports whether a self-comparison occurred anywhere during the test run, serving as a canary if a future implementation reintroduces them.Signed-off-by: Nickolas Goline @nGoline