Subject: [PATCH] unistr/u8-strchr: speed up searching for ASCII characters

* lib/unistr/u8-strchr.c (u8_strchr): Use strchr() for
the single byte case as it was measured to be 50% faster
than the existing code on x86 linux.  Also add a comment
on why not to use memmem() for the moment for the multibyte case.
---
 ChangeLog              |    4 ++++
 lib/unistr/u8-strchr.c |   19 +++++++------------
 2 files changed, 11 insertions(+), 12 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index afcae28..8ca0bd7 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,7 @@
+2010-07-07  Pádraig Brady  <p...@draigbrady.com>
+
+       * lib/unistr/u8-strchr.c (u8_strchr): Use strchr() as it's faster
+
 2010-07-04  Bruno Haible  <br...@clisp.org>

        fsusage: Clarify which code applies to which platforms.
diff --git a/lib/unistr/u8-strchr.c b/lib/unistr/u8-strchr.c
index 3be14c7..3dbd3ca 100644
--- a/lib/unistr/u8-strchr.c
+++ b/lib/unistr/u8-strchr.c
@@ -21,25 +21,20 @@
 /* Specification.  */
 #include "unistr.h"

+#include <string.h>
+
 uint8_t *
 u8_strchr (const uint8_t *s, ucs4_t uc)
 {
   uint8_t c[6];

   if (uc < 0x80)
-    {
-      uint8_t c0 = uc;
-
-      for (;; s++)
-        {
-          if (*s == c0)
-            break;
-          if (*s == 0)
-            goto notfound;
-        }
-      return (uint8_t *) s;
-    }
+    return strchr (s, uc);
   else
+    /* The following is equivalent to:
+         return memmem (s, strlen(s), c, csize);
+       but faster for long S with matching UC near the start,
+       and also memmem is sometimes buggy and inefficient.  */
     switch (u8_uctomb_aux (c, uc, 6))
       {
       case 2:
-- 
1.6.2.5


Reply via email to