public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
From: Adhemerval Zanella <adhemerval.zanella@linaro.org>
To: libc-alpha@sourceware.org,
	Noah Goldstein <goldstein.w.n@gmail.com>,
	Paul Eggert <eggert@cs.ucla.edu>,
	Florian Weimer <fweimer@redhat.com>
Subject: [PATCH v8 0/7] Use introsort for qsort
Date: Mon,  2 Oct 2023 16:33:04 -0300	[thread overview]
Message-ID: <20231002193311.3985890-1-adhemerval.zanella@linaro.org> (raw)

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


             reply	other threads:[~2023-10-02 19:33 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-10-02 19:33 Adhemerval Zanella [this message]
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 18:13 ` Noah Goldstein
2023-10-27 20:24   ` Adhemerval Zanella Netto
2023-10-28  1:31     ` Noah Goldstein

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20231002193311.3985890-1-adhemerval.zanella@linaro.org \
    --to=adhemerval.zanella@linaro.org \
    --cc=eggert@cs.ucla.edu \
    --cc=fweimer@redhat.com \
    --cc=goldstein.w.n@gmail.com \
    --cc=libc-alpha@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).