public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
* [PATCH v8 0/7] Use introsort for qsort
@ 2023-10-02 19:33 Adhemerval Zanella
  2023-10-02 19:33 ` [PATCH v8 1/7] string: Add internal memswap implementation Adhemerval Zanella
                   ` (6 more replies)
  0 siblings, 7 replies; 12+ messages in thread
From: Adhemerval Zanella @ 2023-10-02 19:33 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 v7:
- Move __memswap to a static inline on its own header.
- Improve some comments.

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.

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] 12+ messages in thread
* [PATCH v8 0/7] Use introsort for qsort
@ 2023-10-03 12:22 Adhemerval Zanella
  2023-10-03 12:22 ` [PATCH v8 4/7] stdlib: qsort: Move some macros to inline function Adhemerval Zanella
  0 siblings, 1 reply; 12+ messages in thread
From: Adhemerval Zanella @ 2023-10-03 12:22 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 v7:
- Move __memswap to a static inline on its own header.
- Improve some comments.

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.

PS: this is a update for
https://sourceware.org/pipermail/libc-alpha/2023-October/151887.html
which should be ignored.

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 -
 manual/argp.texi          |   2 +-
 manual/locale.texi        |   3 +-
 manual/search.texi        |   7 +-
 stdlib/Makefile           |   3 +-
 stdlib/msort.c            | 309 --------------------------------
 stdlib/qsort.c            | 318 +++++++++++++++++++++++++--------
 stdlib/tst-qsort3.c       | 366 ++++++++++++++++++++++++++++++++++++++
 string/Makefile           |  12 ++
 string/test-memswap.c     | 192 ++++++++++++++++++++
 sysdeps/generic/memswap.h |  41 +++++
 11 files changed, 856 insertions(+), 399 deletions(-)
 delete mode 100644 stdlib/msort.c
 create mode 100644 stdlib/tst-qsort3.c
 create mode 100644 string/test-memswap.c
 create mode 100644 sysdeps/generic/memswap.h

-- 
2.34.1


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

end of thread, other threads:[~2023-10-30 19:03 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-10-02 19:33 [PATCH v8 0/7] Use introsort for qsort Adhemerval Zanella
2023-10-02 19:33 ` [PATCH v8 1/7] string: Add internal memswap implementation Adhemerval Zanella
2023-10-03  5:43   ` Noah Goldstein
2023-10-03 11:00     ` Adhemerval Zanella Netto
2023-10-02 19:33 ` [PATCH v8 2/7] stdlib: Optimization qsort{_r} swap implementation Adhemerval Zanella
2023-10-02 19:33 ` [PATCH v8 3/7] stdlib: Move insertion sort out qsort Adhemerval Zanella
2023-10-02 19:33 ` [PATCH v8 4/7] stdlib: qsort: Move some macros to inline function Adhemerval Zanella
2023-10-02 19:33 ` [PATCH v8 5/7] stdlib: Implement introsort for qsort (BZ 19305) Adhemerval Zanella
2023-10-02 19:33 ` [PATCH v8 6/7] stdlib: Remove use of mergesort on qsort (BZ 21719) Adhemerval Zanella
2023-10-02 19:33 ` [PATCH v8 7/7] stdlib: Add more qsort{_r} coverage Adhemerval Zanella
2023-10-03 12:22 [PATCH v8 0/7] Use introsort for qsort Adhemerval Zanella
2023-10-03 12:22 ` [PATCH v8 4/7] stdlib: qsort: Move some macros to inline function Adhemerval Zanella
2023-10-30 19:03   ` 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).