about summary refs log tree commit diff
path: root/include
diff options
context:
space:
mode:
authorAdhemerval Zanella <adhemerval.zanella@linaro.org>2023-10-03 09:22:50 -0300
committerAdhemerval Zanella <adhemerval.zanella@linaro.org>2023-10-31 14:18:05 -0300
commit03bf8357e8291857a435afcc3048e0b697b6cc04 (patch)
treed4f97004d712dd066458d15f9be599fc964d5d7b /include
parent274a46c9b25ab733a1fb9fb1497f1beecae30193 (diff)
downloadglibc-03bf8357e8291857a435afcc3048e0b697b6cc04.tar.gz
glibc-03bf8357e8291857a435afcc3048e0b697b6cc04.tar.xz
glibc-03bf8357e8291857a435afcc3048e0b697b6cc04.zip
stdlib: Remove use of mergesort on qsort (BZ 21719)
This patch removes the mergesort optimization on qsort implementation
and uses the introsort instead.  The mergesort implementation has some
issues:

  - It is as-safe only for certain types sizes (if total size is less
    than 1 KB with large element sizes also forcing memory allocation)
    which contradicts the function documentation.  Although not required
    by the C standard, it is preferable and doable to have an O(1) space
    implementation.

  - The malloc for certain element size and element number adds
    arbitrary latency (might even be worse if malloc is interposed).

  - To avoid trigger swap from memory allocation the implementation
    relies on system information that might be virtualized (for instance
    VMs with overcommit memory) which might lead to potentially use of
    swap even if system advertise more memory than actually has.  The
    check also have the downside of issuing syscalls where none is
    expected (although only once per execution).

  - The mergesort is suboptimal on an already sorted array (BZ#21719).

The introsort implementation is already optimized to use constant extra
space (due to the limit of total number of elements from maximum VM
size) and thus can be used to avoid the malloc usage issues.

Resulting performance is slower due the usage of qsort, specially in the
worst-case scenario (partialy or sorted arrays) and due the fact
mergesort uses a slight improved swap operations.

This change also renders the BZ#21719 fix unrequired (since it is meant
to fix the sorted input performance degradation for mergesort).  The
manual is also updated to indicate the function is now async-cancel
safe.

Checked on x86_64-linux-gnu.
Reviewed-by: Noah Goldstein <goldstein.w.n@gmail.com>
Diffstat (limited to 'include')
-rw-r--r--include/stdlib.h2
1 files changed, 0 insertions, 2 deletions
diff --git a/include/stdlib.h b/include/stdlib.h
index 0ed8271d9b..580da9be15 100644
--- a/include/stdlib.h
+++ b/include/stdlib.h
@@ -149,8 +149,6 @@ extern int __posix_openpt (int __oflag) attribute_hidden;
 extern int __add_to_environ (const char *name, const char *value,
 			     const char *combines, int replace)
      attribute_hidden;
-extern void _quicksort (void *const pbase, size_t total_elems,
-			size_t size, __compar_d_fn_t cmp, void *arg);
 
 extern int __on_exit (void (*__func) (int __status, void *__arg), void *__arg);