public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] memchr (trivial) optimization
@ 2007-08-22  9:34 lode leroy
  2007-08-22 10:56 ` Andi Kleen
  2007-08-23  0:13 ` Ingo Oeser
  0 siblings, 2 replies; 8+ messages in thread
From: lode leroy @ 2007-08-22  9:34 UTC (permalink / raw)
  To: linux-kernel

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)

$ diff -Nurp linux/lib/string.c{-old,}
--- linux/lib/string.c-old      2007-08-22 11:18:54.000000000 +0200
+++ linux/lib/string.c  2007-08-22 11:19:20.000000000 +0200
@@ -623,10 +623,12 @@ EXPORT_SYMBOL(strstr);
void *memchr(const void *s, int c, size_t n)
{
        const unsigned char *p = s;
-       while (n-- != 0) {
-               if ((unsigned char)c == *p++) {
-                       return (void *)(p - 1);
+       while (n != 0) {
+               if ((unsigned char)c == *p) {
+                       return (void *)p;
                }
+               n--;
+               p++;
        }
        return NULL;
}

_________________________________________________________________
A lot of passions?  Collect all your personal info on one central location , 
for free! http://get.live.com/live/features


^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2007-08-24 15:56 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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
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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox