mailing list of musl libc
 help / color / mirror / code / Atom feed
From: Rich Felker <dalias@libc.org>
To: musl@lists.openwall.com
Subject: Re: Wrong info in libc comparison
Date: Wed, 13 Sep 2017 14:10:10 -0400	[thread overview]
Message-ID: <20170913181010.GS1627@brightrain.aerifal.cx> (raw)
In-Reply-To: <20170913135154.pfwpg7f32nv4dhja@voyager>

On Wed, Sep 13, 2017 at 03:51:54PM +0200, Markus Wichmann wrote:
> Hello,
> 
> there's a mistake on the libc comparison page
> http://www.etalabs.net/compare_libcs.html: Namely it states that glibc
> uses introsort as sorting algorithm. It doesn't. Glibc uses a
> bog-standard merge sort as main sorting algorithm. A major part of the
> implementation is actually just devoted to optimized copying, and for
> arrays of large objects it uses an interesting way to indirectly sort
> them (i.e. it then allocates an array of references, sorts the
> references, then uses a clever algorithm to get from sorted references
> to a sorted array). But it's all just a standard merge sort.
> 
> However, merge sort on arrays requires a linear amount of scratch space,
> so this merge sort has to allocate memory. Memory allocation is allowed
> to fail, but sorting isn't, so, as a fallback, in case the allocation
> fails (or would use more than half the physical memory, for some
> reason), it falls back to quicksort. This quicksort is implemented with
> a really funky scheme for an explicit stack (i.e., while I'd use
> 
>     push_total_problem();
>     while (stack_not_empty()) {
>         pop_subprob();
>         if (subprob_worth_bothering_with()) {
>             sort_partition();
>             push_larger_subprob();
>             push_smaller_subprob();
>         }
>     }
> 
> they do something more like:
> 
>     push_pseudo_problem();
>     while (stack_not_empty()) {
>         if (subprob_worth_bothering_with()) {
>             sort_partition();
>             figure_out_next_subproblem();
>             then_maybe_push_or_pop_stuff();
>         }
>     }
> 
> ), a median-of-three pivot selection, two-way partitioning (why couldn't
> you be perfect for me?), and a minimum partition size of 4,
> necessitating an insertion sort stage afterwards.
> 
> So, yeah, no introsort in sight. Introsort would be merge sort on large
> arrays, then quicksort on smaller partitions, and finally insertion sort
> for the smallest partitions.

I'm not sure we agree on what introsort means -- normally I take it to
mean doing an O(n²) algorithm with good "typical case" performance to
begin with, but switching to an O(log n) algorithm with a worse
constant factor as soon as it detects a risk that time will grow
quadratically. Normally this is something like starting with quicksort
and possibly switching to heapsort, and my understanding at the time
was that glibc was doing that or something similar, and AFAIK it still
is in the general case where there's insufficient memory for a merge
sort. Does that sound incorrect?

Rich


  reply	other threads:[~2017-09-13 18:10 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-09-13 13:51 Markus Wichmann
2017-09-13 18:10 ` Rich Felker [this message]
2017-09-13 18:51   ` Markus Wichmann
2017-09-13 19:25     ` Szabolcs Nagy
2017-09-13 19:53       ` Rich Felker
2017-09-15 19:18         ` Markus Wichmann
2017-09-16  9:37           ` Szabolcs Nagy
2017-09-16 14:01             ` Markus Wichmann
2017-09-16 17:11               ` Szabolcs Nagy
2017-09-16 19:34                 ` Markus Wichmann
2017-09-16 16:18             ` Rich Felker
2017-09-16 18:38               ` Markus Wichmann

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=20170913181010.GS1627@brightrain.aerifal.cx \
    --to=dalias@libc.org \
    --cc=musl@lists.openwall.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.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

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 NNTP newsgroup(s).