From: Noah Goldstein via Libc-alpha <libc-alpha@sourceware.org>
To: Adhemerval Zanella <adhemerval.zanella@linaro.org>
Cc: GNU C Library <libc-alpha@sourceware.org>
Subject: Re: [PATCH v3 6/7] stdlib: Implement introsort with qsort
Date: Tue, 12 Oct 2021 23:53:07 -0400 [thread overview]
Message-ID: <CAFUsyf+R7q7tcihJX48t3z-Oy1B-yuiosufwg13T7NxKhM=sWA@mail.gmail.com> (raw)
In-Reply-To: <20210903171144.952737-7-adhemerval.zanella@linaro.org>
On Fri, Sep 3, 2021 at 1:16 PM Adhemerval Zanella via Libc-alpha <
libc-alpha@sourceware.org> wrote:
> This patch adds a introsort implementation on qsort to avoid worse-case
> performance of quicksort to O(nlog n). The heapsort fallback used is a
> heapsort based on Linux implementation (commit 22a241ccb2c19962a). As a
> side note the introsort implementation is similar the one used on
> libstdc++ for std::sort.
>
> Checked on x86_64-linux-gnu.
> ---
> stdlib/qsort.c | 94 ++++++++++++++++++++++++++++++++++++++++++++++----
> 1 file changed, 87 insertions(+), 7 deletions(-)
>
> diff --git a/stdlib/qsort.c b/stdlib/qsort.c
> index 5df640362d..8368576aae 100644
> --- a/stdlib/qsort.c
> +++ b/stdlib/qsort.c
> @@ -113,6 +113,7 @@ typedef struct
> {
> char *lo;
> char *hi;
> + size_t depth;
>
Why do a depth tracker per stack_node as opposed to one for
the entire call?
> } stack_node;
>
> /* The stack needs log (total_elements) entries (we could even subtract
> @@ -122,23 +123,92 @@ typedef struct
> enum { STACK_SIZE = CHAR_BIT * sizeof (size_t) };
>
> static inline stack_node *
> -push (stack_node *top, char *lo, char *hi)
> +push (stack_node *top, char *lo, char *hi, size_t depth)
> {
> top->lo = lo;
> top->hi = hi;
> + top->depth = depth;
> return ++top;
> }
>
> static inline stack_node *
> -pop (stack_node *top, char **lo, char **hi)
> +pop (stack_node *top, char **lo, char **hi, size_t *depth)
> {
> --top;
> *lo = top->lo;
> *hi = top->hi;
> + *depth = top->depth;
> return top;
> }
>
>
> +/* A fast, small, non-recursive O(nlog n) heapsort, adapted from Linux
> + lib/sort.c. Used on introsort implementation as a fallback routine
> with
> + worst-case performance of O(nlog n) and worst-case space complexity of
> + O(1). */
> +
> +static inline size_t
> +parent (size_t i, unsigned int lsbit, size_t size)
> +{
> + i -= size;
> + i -= size & -(i & lsbit);
> + return i / 2;
> +}
> +
> +static void
> +heapsort_r (void *base, void *end, size_t size, swap_func_t swap_func,
> + __compar_d_fn_t cmp, void *arg)
> +{
> + size_t num = ((uintptr_t) end - (uintptr_t) base) / size;
> + size_t n = num * size, a = (num/2) * size;
> + /* Used to find parent */
> + const unsigned int lsbit = size & -size;
> +
> + /* num < 2 || size == 0. */
> + if (a == 0)
> + return;
> +
> + for (;;)
> + {
> + size_t b, c, d;
> +
> + if (a != 0)
> + /* Building heap: sift down --a */
> + a -= size;
> + else if (n -= size)
> + /* Sorting: Extract root to --n */
> + do_swap (base, base + n, size, swap_func);
> + else
> + break;
> +
> + /* Sift element at "a" down into heap. This is the "bottom-up"
> variant,
> + which significantly reduces calls to cmp_func(): we find the
> sift-down
> + path all the way to the leaves (one compare per level), then
> backtrack
> + to find where to insert the target element.
> +
> + Because elements tend to sift down close to the leaves, this uses
> fewer
> + compares than doing two per level on the way down. (A bit more
> than
> + half as many on average, 3/4 worst-case.). */
> + for (b = a; c = 2 * b + size, (d = c + size) < n;)
> + b = cmp (base + c, base + d, arg) >= 0 ? c : d;
> + if (d == n)
> + /* Special case last leaf with no sibling. */
> + b = c;
> +
> + /* Now backtrack from "b" to the correct location for "a". */
> + while (b != a && cmp (base + a, base + b, arg) >= 0)
> + b = parent (b, lsbit, size);
> + /* Where "a" belongs. */
> + c = b;
> + while (b != a)
> + {
> + /* Shift it into place. */
> + b = parent (b, lsbit, size);
> + do_swap (base + b, base + c, size, swap_func);
> + }
> + }
> +}
> +
> /* Order size using quicksort. This implementation incorporates
> four optimizations discussed in Sedgewick:
>
> @@ -223,7 +293,7 @@ _quicksort (void *const pbase, size_t total_elems,
> size_t size,
>
> const size_t max_thresh = MAX_THRESH * size;
>
> - if (total_elems == 0)
> + if (total_elems <= 1)
> /* Avoid lossage with unsigned arithmetic below. */
> return;
>
> @@ -235,6 +305,9 @@ _quicksort (void *const pbase, size_t total_elems,
> size_t size,
> else
> swap_func = SWAP_BYTES;
>
> + /* Maximum depth before quicksort switches to heapsort. */
> + size_t depth = 2 * (CHAR_BIT - 1 - __builtin_clzl (total_elems));
> +
> if (total_elems > MAX_THRESH)
> {
> char *lo = base_ptr;
> @@ -242,10 +315,17 @@ _quicksort (void *const pbase, size_t total_elems,
> size_t size,
> stack_node stack[STACK_SIZE];
> stack_node *top = stack;
>
> - top = push (top, NULL, NULL);
> + top = push (top, NULL, NULL, depth);
>
> while (stack < top)
> {
> + if (depth == 0)
> + {
+ heapsort_r (lo, hi, size, swap_func, cmp, arg);
> + top = pop (top, &lo, &hi, &depth);
> + continue;
> + }
> +
> char *left_ptr;
> char *right_ptr;
>
> @@ -309,7 +389,7 @@ _quicksort (void *const pbase, size_t total_elems,
> size_t size,
> {
> if ((size_t) (hi - left_ptr) <= max_thresh)
> /* Ignore both small partitions. */
> - top = pop (top, &lo, &hi);
> + top = pop (top, &lo, &hi, &depth);
> else
> /* Ignore small left partition. */
> lo = left_ptr;
> @@ -320,13 +400,13 @@ _quicksort (void *const pbase, size_t total_elems,
> size_t size,
> else if ((right_ptr - lo) > (hi - left_ptr))
>
Since we now have a depth counter, is it faster to still
always select the bigger region for the stack or
to remove this branch and just choose a direction?
We should be able to bound the size of the stack structure
with depth.
> {
> /* Push larger left partition indices. */
> - top = push (top, lo, right_ptr);
> + top = push (top, lo, right_ptr, depth - 1);
> lo = left_ptr;
> }
> else
> {
> /* Push larger right partition indices. */
> - top = push (top, left_ptr, hi);
> + top = push (top, left_ptr, hi, depth - 1);
> hi = right_ptr;
> }
> }
> --
> 2.30.2
>
>
next prev parent reply other threads:[~2021-10-13 3:53 UTC|newest]
Thread overview: 41+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-09-03 17:11 [PATCH v3 0/7] Use introsort for qsort Adhemerval Zanella via Libc-alpha
2021-09-03 17:11 ` [PATCH v3 1/7] benchtests: Add bench-qsort Adhemerval Zanella via Libc-alpha
2021-09-04 9:09 ` Alexander Monakov via Libc-alpha
2021-09-06 18:30 ` Adhemerval Zanella via Libc-alpha
2021-10-13 3:19 ` Noah Goldstein via Libc-alpha
2021-10-15 12:52 ` Adhemerval Zanella via Libc-alpha
2021-10-15 16:39 ` Noah Goldstein via Libc-alpha
2021-10-15 17:19 ` Adhemerval Zanella via Libc-alpha
2021-09-03 17:11 ` [PATCH v3 2/7] support: Fix getopt_long with CMDLINE_OPTIONS Adhemerval Zanella via Libc-alpha
2021-09-03 17:11 ` [PATCH v3 3/7] stdlib: Optimization qsort{_r} swap implementation (BZ #19305) Adhemerval Zanella via Libc-alpha
2021-10-13 3:29 ` Noah Goldstein via Libc-alpha
2021-10-13 3:39 ` Noah Goldstein via Libc-alpha
2021-10-15 13:29 ` Adhemerval Zanella via Libc-alpha
2021-10-15 17:17 ` Noah Goldstein via Libc-alpha
2021-10-15 17:34 ` Adhemerval Zanella via Libc-alpha
2021-10-15 17:45 ` Noah Goldstein via Libc-alpha
2021-10-15 17:56 ` Adhemerval Zanella via Libc-alpha
2021-10-15 13:12 ` Adhemerval Zanella via Libc-alpha
2021-10-15 16:45 ` Noah Goldstein via Libc-alpha
2021-10-15 17:21 ` Adhemerval Zanella via Libc-alpha
2021-09-03 17:11 ` [PATCH v3 4/7] stdlib: Move insertion sort out qsort Adhemerval Zanella via Libc-alpha
2021-09-06 20:35 ` Fangrui Song via Libc-alpha
2021-09-06 20:48 ` Fangrui Song via Libc-alpha
2021-09-03 17:11 ` [PATCH v3 5/7] stdlib: qsort: Move some macros to inline function Adhemerval Zanella via Libc-alpha
2021-09-03 17:11 ` [PATCH v3 6/7] stdlib: Implement introsort with qsort Adhemerval Zanella via Libc-alpha
2021-09-04 9:17 ` Alexander Monakov via Libc-alpha
2021-09-06 18:43 ` Adhemerval Zanella via Libc-alpha
2021-09-06 20:23 ` Fangrui Song via Libc-alpha
2021-10-13 3:53 ` Noah Goldstein via Libc-alpha [this message]
2021-09-03 17:11 ` [PATCH v3 7/7] stdlib: Remove use of mergesort on qsort (BZ #21719) Adhemerval Zanella via Libc-alpha
2021-09-03 19:18 ` [PATCH v3 0/7] Use introsort for qsort Paul Eggert
2021-09-06 14:13 ` Carlos O'Donell via Libc-alpha
2021-09-06 17:03 ` Zack Weinberg via Libc-alpha
2021-09-06 18:19 ` Adhemerval Zanella via Libc-alpha
2021-09-07 0:14 ` Paul Eggert
2021-09-07 14:32 ` Adhemerval Zanella via Libc-alpha
2021-09-07 17:39 ` Paul Eggert
2021-09-07 18:07 ` Adhemerval Zanella via Libc-alpha
2021-09-07 19:28 ` Paul Eggert
2021-09-08 11:56 ` Adhemerval Zanella via Libc-alpha
2021-09-09 0:39 ` Paul Eggert
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
List information: https://www.gnu.org/software/libc/involved.html
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to='CAFUsyf+R7q7tcihJX48t3z-Oy1B-yuiosufwg13T7NxKhM=sWA@mail.gmail.com' \
--to=libc-alpha@sourceware.org \
--cc=adhemerval.zanella@linaro.org \
--cc=goldstein.w.n@gmail.com \
/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).