mailing list of musl libc
 help / color / mirror / code / Atom feed
From: Ray <i@maskray.me>
To: musl@lists.openwall.com
Subject: Re: [PATCH] bsearch: simplify and optimize
Date: Sun, 22 Jul 2018 16:03:34 -0700	[thread overview]
Message-ID: <CAN30aBFTNxUPQ6NUXYFPe-g12E0yZHtuSgDaon4g4w0HgbZrQA@mail.gmail.com> (raw)
In-Reply-To: <20180722185131.GW1392@brightrain.aerifal.cx>

[-- Attachment #1: Type: text/plain, Size: 2115 bytes --]

Thanks!

On Sun, Jul 22, 2018 at 11:51 AM, Rich Felker <dalias@libc.org> wrote:

> On Sat, Jul 21, 2018 at 05:47:32PM -0700, Fāng-ruì Sòng wrote:
>
> > >From 64d86f96fa404dbaa40de8a1979ecadca6add4ca Mon Sep 17 00:00:00 2001
> > From: Fangrui Song <i@maskray.me>
> > Date: Sat, 21 Jul 2018 17:34:00 -0700
> > Subject: [PATCH] bsearch: simplify and optimize
> >
> > ---
> >  src/stdlib/bsearch.c | 13 ++++++-------
> >  1 file changed, 6 insertions(+), 7 deletions(-)
> >
> > diff --git a/src/stdlib/bsearch.c b/src/stdlib/bsearch.c
> > index 61d89367..2a998cc6 100644
> > --- a/src/stdlib/bsearch.c
> > +++ b/src/stdlib/bsearch.c
> > @@ -7,14 +7,13 @@ void *bsearch(const void *key, const void *base,
> size_t nel, size_t width, int (
> >       while (nel > 0) {
> >               try = (char *)base + width*(nel/2);
> >               sign = cmp(key, try);
> > -             if (!sign) return try;
> > -             else if (nel == 1) break;
> > -             else if (sign < 0)
> > +             if (sign < 0)
> >                       nel /= 2;
> > -             else {
> > -                     base = try;
> > -                     nel -= nel/2;
> > -             }
> > +             else if (sign > 0) {
> > +                     base = (char *)try + width;
> > +                     nel -= nel/2+1;
> > +             } else
> > +                     return try;
> >       }
> >       return NULL;
> >  }
>
> The change here looks correct -- what it's doing is observing that the
> compared element is the first slot of the second ceil(half) of the
> array, and thus can be removed for further comparison, so that we
> descend into the second ceil(half)-1 rather than ceil(half) elements.
> This change ensures that nel strictly decreases with each iteration,
> so that the case of != but nel==1 does not need to be special-cased
> anymore.
>
> Only change I would make is minor style: generally when if/else
> construct uses braces for one case, we use them for all. If there are
> no other objections I can apply this change when merging.
>
> Rich
>

[-- Attachment #2: Type: text/html, Size: 2878 bytes --]

      reply	other threads:[~2018-07-22 23:03 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-07-22  0:47 Fāng-ruì Sòng
2018-07-22 18:51 ` Rich Felker
2018-07-22 23:03   ` Ray [this message]

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=CAN30aBFTNxUPQ6NUXYFPe-g12E0yZHtuSgDaon4g4w0HgbZrQA@mail.gmail.com \
    --to=i@maskray.me \
    --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).