mailing list of musl libc
 help / color / mirror / code / Atom feed
From: Rich Felker <dalias@libc.org>
To: musl@lists.openwall.com
Subject: Re: [RFC] new qsort implementation
Date: Mon, 1 Sep 2014 16:53:42 -0400	[thread overview]
Message-ID: <20140901205342.GE12888@brightrain.aerifal.cx> (raw)
In-Reply-To: <20140901182005.GA7301@duality.lan>

On Mon, Sep 01, 2014 at 01:20:05PM -0500, Bobby Bingham wrote:
> > > Here are the numbers comparing musl's current smoothsort with the
> > > attached grailsort code for various input patterns and sizes.  The test
> > > was run on x86_64, compiled with gcc 4.8.3 at -Os:
> > >
> > >                           sorted             reverse            constant
> > >                  compares     ms     compares     ms     compares     ms
> > > musl smoothsort     19976      0       268152      8        19976      0
> > >                    199971      2      3327332     59       199971      2
> > >                   1999963     29     40048748    663      1999963     27
> > >                  19999960    289    465600753   7505     19999960    293
> > >
> > > grailsort           71024      0        41110      0        28004      0
> > >                    753996      2       412840      5       270727      3
> > >                   7686249     27      4177007     74      2729965     41
> > >                  75927601    277     42751315    901     28243939    436
> > >
> >
> > interesting that the sorted case is faster with much more compares
> > here on i386 smoothsort is faster
> >
> >                             sorted             reverse            constant
> >                    compares     ms     compares     ms     compares     ms
> > musl smoothsort       19976      0       268152      7        19976      1
> >                      199971      8      3327332    103       199971     15
> >                     1999963    105     40048748   1151      1999963    103
> >                    19999960   1087    465600753  13339     19999960   1103
> >
> > grailsort             71024      1        41110      3        28004      3
> >                      753996     20       412840     23       270727     23
> >                     7686249    151      4177007    370      2729965    224
> >                    75927601   1438     42751315   4507     28243939   2353
> >
> 
> Interesting.  When I saw that grailsort was faster even with more
> comparisons on my machine, I had attributed it to my swap possibly being
> faster.  But I don't see why this wouldn't also be the case on i386, so
> maybe something else is going on.

I think it makes sense to test with two different types of cases:
expensive comparisons (costly compare function) and expensive swaps
(large array elements).

> > > #include <stdlib.h>
> > > #include <limits.h>
> > >
> > > size_t __bsearch(const void *key, const void *base, size_t nel, size_t width, int (*cmp)(const void *, const void *))
> > > {
> > > 	size_t baseidx = 0, tryidx;
> > > 	void *try;
> > > 	int sign;
> > >
> > > 	while (nel > 0) {
> > > 		tryidx = baseidx + nel/2;
> > > 		try = (char*)base + tryidx*width;
> > > 		sign = cmp(key, try);
> > > 		if (!sign) return tryidx;
> > > 		else if (sign < 0)
> > > 			nel /= 2;
> > > 		else {
> > > 			baseidx = tryidx + 1;
> > > 			nel -= nel/2 + 1;
> > > 		}
> > > 	}
> > >
> > > 	return ~baseidx;
> > > }
> > >
> > > void *bsearch(const void *key, const void *base, size_t nel, size_t width, int (*cmp)(const void *, const void *))
> > > {
> > > 	size_t idx = __bsearch(key, base, nel, width, cmp);
> > > 	return idx > SSIZE_MAX ? NULL : (char*)base + idx*width;
> > > }
> >
> > musl does not malloc >=SSIZE_MAX memory, but mmap can so baseidx
> > may be >0x7fffffff on a 32bit system
> >
> > i'm not sure if current qsort handles this case
> 
> I thought I recalled hearing that SSIZE_MAX was the upper bound on all
> object sizes in musl, but if we still allow larger mmaps than that, I
> guess not.  I'll find a different approach when I send the next version
> of the code.

You are correct and nsz is mistaken on this. musl does not permit any
object size larger than SSIZE_MAX. mmap and malloc both enforce this.
But I'm not sure why you've written bsearch to need this assumption.
The bsearch in musl gets by fine without it.

Rich


  reply	other threads:[~2014-09-01 20:53 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-09-01  7:12 Bobby Bingham
2014-09-01 11:17 ` Szabolcs Nagy
2014-09-01 18:20   ` Bobby Bingham
2014-09-01 20:53     ` Rich Felker [this message]
2014-09-01 21:46       ` Bobby Bingham
2014-09-01 11:25 ` Alexander Monakov
2014-09-01 18:27   ` Bobby Bingham
2023-02-17 15:51 ` [musl] " Rich Felker
2023-02-17 22:53   ` Rich Felker

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=20140901205342.GE12888@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).