[libc-commits] [libc] [llvm] [libc] Improve qsort (PR #120450)

Orson Peters via libc-commits libc-commits at lists.llvm.org
Thu Dec 19 14:16:15 PST 2024


================
@@ -19,13 +19,11 @@ LLVM_LIBC_FUNCTION(void, qsort_r,
                    (void *array, size_t array_size, size_t elem_size,
                     int (*compare)(const void *, const void *, void *),
                     void *arg)) {
-  if (array == nullptr || array_size == 0 || elem_size == 0)
-    return;
-  internal::Comparator c(compare, arg);
-  auto arr = internal::Array(reinterpret_cast<uint8_t *>(array), array_size,
-                             elem_size, c);
 
-  internal::sort(arr);
+  internal::unstable_sort(array, array_size, elem_size,
+                          [compare, arg](const void *a, const void *b) -> bool {
+                            return compare(a, b, arg) < 0;
+                          });
----------------
orlp wrote:

@michaelrj-google I know this is ultimately subjective so I can't do better than an appeal to authority, but... as someone that knows a thing or two about sorting, the last decade of sorting theory has firmly discarded the ternary comparison in favor of a binary one. The public interface (obviously) can't be changed to match this, but the implementation however can be modern. I would strongly recommend to use the `is_less` paradigm in the implementation for consistency with all other modern sorting implementations, especially because the implemented sorting algorithm does not use the ternary nature of the comparison function at all.

https://github.com/llvm/llvm-project/pull/120450


More information about the libc-commits mailing list