public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Matt Mackall <mpm@selenic.com>
To: Ingo Oeser <ioe-lkml@rameria.de>
Cc: lode leroy <lode_leroy@hotmail.com>, linux-kernel@vger.kernel.org
Subject: Re: [PATCH] memchr (trivial) optimization
Date: Thu, 23 Aug 2007 19:13:51 -0500	[thread overview]
Message-ID: <20070824001351.GA20266@waste.org> (raw)
In-Reply-To: <200708230213.21332.ioe-lkml@rameria.de>

On Thu, Aug 23, 2007 at 02:13:20AM +0200, Ingo Oeser wrote:
> On Wednesday 22 August 2007, lode leroy wrote:
> > While profiling something completely unrelated, I noticed
> > that on the workloads I used memchr for, I saw a 30%-40% improvement
> > in performance, with the following trivial changes...
> > (basically, it saves 3 operations for each call)
> 
> Yes, but then you could be a bit more explicit to the compiler
> on what you are doing here:
>  
> void *memchr(const void *s, int c, size_t n)
> {
> 	const unsigned char *p = s;
> 
> 	for (; n != 0; n--, p++) {
>                if ((unsigned char)c == *p) {
>                        return (void *)p;
> 	}
> 	return NULL;
> }
> 
> Now the compiler should see the loop more clearly.

And you can do even better with this:

void *memchr(const void *s, int c, size_t n)
{
       const unsigned char *p = s, *e = s + n;
       const unsigned char *e = p + n;

       for (; p < e ; p++)
                if ((unsigned char)c == *p)
                        return (void *)p;

       return NULL;
}

which changes the inner loop from:

  50:   38 08                   cmp    %cl,(%eax)
  52:   74 08                   je     5c <memchr2+0x1a>
  54:   4a                      dec    %edx
  55:   40                      inc    %eax
  56:   85 d2                   test   %edx,%edx
  58:   75 f6                   jne    50 <memchr2+0xe>

to:

  6e:   38 08                   cmp    %cl,(%eax)
  70:   74 07                   je     79 <memchr3+0x1b>
  72:   40                      inc    %eax
  73:   39 d0                   cmp    %edx,%eax
  75:   72 f7                   jb     6e <memchr3+0x10>


-- 
Mathematics is the supreme nostalgia of our time.

  reply	other threads:[~2007-08-24  0:12 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-08-22  9:34 [PATCH] memchr (trivial) optimization lode leroy
2007-08-22 10:56 ` Andi Kleen
2007-08-23  0:13 ` Ingo Oeser
2007-08-24  0:13   ` Matt Mackall [this message]
2007-08-24  1:03     ` Jeremy Fitzhardinge
2007-08-24  2:19       ` Matt Mackall
2007-08-24 12:54     ` Jan Engelhardt
2007-08-24 15:57       ` Matt Mackall

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=20070824001351.GA20266@waste.org \
    --to=mpm@selenic.com \
    --cc=ioe-lkml@rameria.de \
    --cc=linux-kernel@vger.kernel.org \
    --cc=lode_leroy@hotmail.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.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox