public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
* [PATCH v7 0/7] Use introsort for qsort
@ 2023-09-14 18:36 Adhemerval Zanella
  2023-09-14 18:36 ` [PATCH v7 1/7] string: Add internal memswap implementation Adhemerval Zanella
                   ` (6 more replies)
  0 siblings, 7 replies; 14+ messages in thread
From: Adhemerval Zanella @ 2023-09-14 18:36 UTC (permalink / raw)
  To: libc-alpha, Noah Goldstein, Paul Eggert, Florian Weimer

The motivation for using introsort is to make it fully AS-safe and
AC-Safe, with a limited stack size requirement, to remove the use of
malloc (which is troublesome since it seems some programs 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, instead it uses used a well-understood introsort (used on
libstdc++, for instance) and leveraged the current quicksort
implementation along with a heapsort one from Linux kernel.

Performance-wise, the introsort does fall short compared to the
mergesort [1].  I have not added a benchmark because I think this should
not be the focus of this patch.

Changes from v6:
- Added internal __memswap symbol.
- Improved tst-qsort3 with a reference implementation and a new
  duplicated input.

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 (7):
  string: Add internal memswap implementation
  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 -
 include/string.h      |   3 +
 manual/argp.texi      |   2 +-
 manual/locale.texi    |   3 +-
 manual/search.texi    |   7 +-
 stdlib/Makefile       |   3 +-
 stdlib/msort.c        | 309 -----------------------------------
 stdlib/qsort.c        | 315 +++++++++++++++++++++++++++---------
 stdlib/tst-qsort3.c   | 366 ++++++++++++++++++++++++++++++++++++++++++
 string/Makefile       |  13 ++
 string/memswap.c      |  41 +++++
 string/test-memswap.c | 191 ++++++++++++++++++++++
 12 files changed, 856 insertions(+), 399 deletions(-)
 delete mode 100644 stdlib/msort.c
 create mode 100644 stdlib/tst-qsort3.c
 create mode 100644 string/memswap.c
 create mode 100644 string/test-memswap.c

-- 
2.34.1


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

end of thread, other threads:[~2023-09-20 14:12 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-09-14 18:36 [PATCH v7 0/7] Use introsort for qsort Adhemerval Zanella
2023-09-14 18:36 ` [PATCH v7 1/7] string: Add internal memswap implementation Adhemerval Zanella
2023-09-15 14:36   ` Noah Goldstein
2023-09-19 13:18     ` Adhemerval Zanella Netto
2023-09-15 15:00   ` Cristian Rodríguez
2023-09-14 18:36 ` [PATCH v7 2/7] stdlib: Optimization qsort{_r} swap implementation Adhemerval Zanella
2023-09-14 18:37 ` [PATCH v7 3/7] stdlib: Move insertion sort out qsort Adhemerval Zanella
2023-09-14 18:37 ` [PATCH v7 4/7] stdlib: qsort: Move some macros to inline function Adhemerval Zanella
2023-09-14 18:37 ` [PATCH v7 5/7] stdlib: Implement introsort for qsort (BZ 19305) Adhemerval Zanella
2023-09-15 14:42   ` Noah Goldstein
2023-09-20 13:50     ` Adhemerval Zanella Netto
2023-09-20 14:12       ` Noah Goldstein
2023-09-14 18:37 ` [PATCH v7 6/7] stdlib: Remove use of mergesort on qsort (BZ 21719) Adhemerval Zanella
2023-09-14 18:37 ` [PATCH v7 7/7] stdlib: Add more qsort{_r} coverage Adhemerval Zanella

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).