mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Alexey Dobriyan <adobriyan@gmail.com>
To: joe@perches.com
Cc: linux-kernel@vger.kernel.org, linux@rasmusvillemoes.dk,
	akpm@linux-foundation.org
Subject: Re: [PATCH] un-improve strrchr()
Date: Sun, 28 Jun 2015 22:43:14 +0300	[thread overview]
Message-ID: <20150628194314.GA16858@p183.telecom.by> (raw)
In-Reply-To: <20150628164403.GA7169@p183.telecom.by>

Joe Perches wrote:

> On Sun, 2015-06-28 at 19:44 +0300, Alexey Dobriyan wrote:
> > Commit 8da53d4595a53fb9a3380dd4d1c9bc24c7c9aab8
> > ("lib/string.c: improve strrchr()") changed strrchr() implementation
> > from "rewind to the end and search backwards" to "search forward"
> > optimizing for characher not found case. However, common case is exactly
> > the opposite: string is absolute pathname, c is '/' always to be found.
> > 
> > Previous code did 1 branch per character + 1 branch for every character
> > in the last path component. Current code does 2 branches per characher
> > regardless.
> 
> Are you comparing total cycles of all of the branches
> in the called functions too?

I'm comparing branches to branches. For strrchr() you don't need 2xN branches
even in theory. strlen() might even be optimized (gcc does have the impudence
to inline it's own CMPB version though).

> As written the current version removes the strlen call.

It does.

> > --- a/lib/string.c
> > +++ b/lib/string.c
> > @@ -313,12 +313,13 @@ EXPORT_SYMBOL(strchrnul);
> >   */
> >  char *strrchr(const char *s, int c)
> >  {
> > -	const char *last = NULL;
> > +	const char *p = s + strlen(s);
> > +
> >  	do {
> > -		if (*s == (char)c)
> > -			last = s;
> > -	} while (*s++);
> > -	return (char *)last;
> > +		if (*p == (char)c)
> > +			return (char *)p;
> > +	} while (--p >= s);
> > +	return NULL;
> >  }
> >  EXPORT_SYMBOL(strrchr);
> >  #endif

  parent reply	other threads:[~2015-06-28 19:43 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <20150628163252.GA1991@p183.telecom.by>
2015-06-28 16:44 ` Alexey Dobriyan
2015-06-28 18:01   ` Joe Perches
2015-06-28 19:43   ` Alexey Dobriyan [this message]
2015-06-30 23:52   ` Chris Rorvick
2015-07-01 14:07     ` Alexey Dobriyan

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=20150628194314.GA16858@p183.telecom.by \
    --to=adobriyan@gmail.com \
    --cc=akpm@linux-foundation.org \
    --cc=joe@perches.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux@rasmusvillemoes.dk \
    /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

all inboxes | Powered by JetHome®