public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
* [PATCH v6 0/6] Use introsort for qsort
@ 2023-07-18 14:15 Adhemerval Zanella
  2023-07-18 14:15 ` [PATCH v6 1/6] stdlib: Optimization qsort{_r} swap implementation Adhemerval Zanella
                   ` (5 more replies)
  0 siblings, 6 replies; 23+ messages in thread
From: Adhemerval Zanella @ 2023-07-18 14:15 UTC (permalink / raw)
  To: libc-alpha, Paul Eggert, Alexander Monakov

The main motivation to use introsort is to make it fully AS-Safe and
AC-Safe, with a limited stack size requirement, to remove the use
of malloc (which troublesome since it seems some program do longjmp
from the callback comparison program), and keep worst-case scenario
bounded to O(n*ln(n)) (instead of potentially quadradic as for the
quicksort).

The implementation does not aim to be the state-of-the-art sort
algorithm, rather I used a well understood introsort (used on
libstdc++, for instance) and leverage the current quicksort
implementation along with a heapsort one from Linux kernel.

Performance-wise, the introsort does fall short compare to
mergesort [1].  I have not added the benchmark because I see that
this should not be focus of this patch. I have added some simple
optimization, also used by Linux kernel heapsort.

Changes from v5:
- Rewrite heapsort to a custom implementation.
- Use may_alias attribute on swap optimization.

Changes from v4
- Use enum for swap function selection.
- Simplify is_aligned.
- Renamed insertsort.

[1] https://sourceware.org/pipermail/libc-alpha/2021-September/130698.html

Adhemerval Zanella (6):
  stdlib: Optimization qsort{_r} swap implementation
  stdlib: Move insertion sort out qsort
  stdlib: qsort: Move some macros to inline function
  stdlib: Implement introsort for qsort (BZ 19305)
  stdlib: Remove use of mergesort on qsort (BZ 21719)
  stdlib: Add more qsort{_r} coverage

 include/stdlib.h    |   2 -
 manual/argp.texi    |   2 +-
 manual/locale.texi  |   3 +-
 manual/search.texi  |   7 +-
 stdlib/Makefile     |   3 +-
 stdlib/msort.c      | 309 ----------------------------------------
 stdlib/qsort.c      | 337 +++++++++++++++++++++++++++++++++-----------
 stdlib/tst-qsort3.c | 298 +++++++++++++++++++++++++++++++++++++++
 8 files changed, 562 insertions(+), 399 deletions(-)
 delete mode 100644 stdlib/msort.c
 create mode 100644 stdlib/tst-qsort3.c

-- 
2.34.1


^ permalink raw reply	[flat|nested] 23+ messages in thread

end of thread, other threads:[~2023-08-31 17:38 UTC | newest]

Thread overview: 23+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-07-18 14:15 [PATCH v6 0/6] Use introsort for qsort Adhemerval Zanella
2023-07-18 14:15 ` [PATCH v6 1/6] stdlib: Optimization qsort{_r} swap implementation Adhemerval Zanella
2023-08-22 19:31   ` Adhemerval Zanella Netto
2023-08-29  7:33   ` Noah Goldstein
2023-08-29 13:56     ` Adhemerval Zanella Netto
2023-08-29 16:53   ` Florian Weimer
2023-08-29 22:52     ` Noah Goldstein
2023-08-30 13:51       ` Adhemerval Zanella Netto
2023-07-18 14:15 ` [PATCH v6 2/6] stdlib: Move insertion sort out qsort Adhemerval Zanella
2023-08-29  7:39   ` Noah Goldstein
2023-08-29 13:59     ` Adhemerval Zanella Netto
2023-07-18 14:15 ` [PATCH v6 3/6] stdlib: qsort: Move some macros to inline function Adhemerval Zanella
2023-07-18 14:15 ` [PATCH v6 4/6] stdlib: Implement introsort for qsort (BZ 19305) Adhemerval Zanella
2023-07-18 17:37   ` Noah Goldstein
2023-07-18 19:08     ` Adhemerval Zanella Netto
2023-08-29  7:45       ` Noah Goldstein
2023-08-31 12:13         ` Adhemerval Zanella Netto
2023-08-31 17:38           ` Noah Goldstein
2023-07-18 14:15 ` [PATCH v6 5/6] stdlib: Remove use of mergesort on qsort (BZ 21719) Adhemerval Zanella
2023-07-18 14:15 ` [PATCH v6 6/6] stdlib: Add more qsort{_r} coverage Adhemerval Zanella
2023-08-29  7:57   ` Noah Goldstein
2023-08-30 17:03     ` Adhemerval Zanella Netto
2023-08-30 18:49       ` Noah Goldstein

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).