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

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?

As written the current version removes the strlen call.

> --- 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



  reply	other threads:[~2015-06-28 18:02 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 [this message]
2015-06-28 19:43   ` Alexey Dobriyan
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=1435514515.9587.46.camel@perches.com \
    --to=joe@perches.com \
    --cc=adobriyan@gmail.com \
    --cc=akpm@linux-foundation.org \
    --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

Powered by JetHome