dfa500
commit 284f42bc778e487dfd5dff5c01959f93b9e0c4f5
dfa500
Author: Wilco Dijkstra <wdijkstr@arm.com>
dfa500
Date:   Fri Aug 3 17:24:12 2018 +0100
dfa500
dfa500
    Simplify and speedup strstr/strcasestr first match
dfa500
    
dfa500
    Looking at the benchtests, both strstr and strcasestr spend a lot of time
dfa500
    in a slow initialization loop handling one character per iteration.
dfa500
    This can be simplified and use the much faster strlen/strnlen/strchr/memcmp.
dfa500
    Read ahead a few cachelines to reduce the number of strnlen calls, which
dfa500
    improves performance by ~3-4%.  This patch improves the time taken for the
dfa500
    full strstr benchtest by >40%.
dfa500
    
dfa500
            * string/strcasestr.c (STRCASESTR): Simplify and speedup first match.
dfa500
            * string/strstr.c (AVAILABLE): Likewise.
dfa500
dfa500
diff --git a/string/strcasestr.c b/string/strcasestr.c
dfa500
index 421764bd1b0ff22e..8aa76037dcc052f3 100644
dfa500
--- a/string/strcasestr.c
dfa500
+++ b/string/strcasestr.c
dfa500
@@ -59,31 +59,22 @@
dfa500
    case-insensitive comparison.  This function gives unspecified
dfa500
    results in multibyte locales.  */
dfa500
 char *
dfa500
-STRCASESTR (const char *haystack_start, const char *needle_start)
dfa500
+STRCASESTR (const char *haystack, const char *needle)
dfa500
 {
dfa500
-  const char *haystack = haystack_start;
dfa500
-  const char *needle = needle_start;
dfa500
   size_t needle_len; /* Length of NEEDLE.  */
dfa500
   size_t haystack_len; /* Known minimum length of HAYSTACK.  */
dfa500
-  bool ok = true; /* True if NEEDLE is prefix of HAYSTACK.  */
dfa500
-
dfa500
-  /* Determine length of NEEDLE, and in the process, make sure
dfa500
-     HAYSTACK is at least as long (no point processing all of a long
dfa500
-     NEEDLE if HAYSTACK is too short).  */
dfa500
-  while (*haystack && *needle)
dfa500
-    {
dfa500
-      ok &= (TOLOWER ((unsigned char) *haystack)
dfa500
-	     == TOLOWER ((unsigned char) *needle));
dfa500
-      haystack++;
dfa500
-      needle++;
dfa500
-    }
dfa500
-  if (*needle)
dfa500
+
dfa500
+  /* Handle empty NEEDLE special case.  */
dfa500
+  if (needle[0] == '\0')
dfa500
+    return (char *) haystack;
dfa500
+
dfa500
+  /* Ensure HAYSTACK length is at least as long as NEEDLE length.
dfa500
+     Since a match may occur early on in a huge HAYSTACK, use strnlen
dfa500
+     and read ahead a few cachelines for improved performance.  */
dfa500
+  needle_len = strlen (needle);
dfa500
+  haystack_len = __strnlen (haystack, needle_len + 256);
dfa500
+  if (haystack_len < needle_len)
dfa500
     return NULL;
dfa500
-  if (ok)
dfa500
-    return (char *) haystack_start;
dfa500
-  needle_len = needle - needle_start;
dfa500
-  haystack = haystack_start + 1;
dfa500
-  haystack_len = needle_len - 1;
dfa500
 
dfa500
   /* Perform the search.  Abstract memory is considered to be an array
dfa500
      of 'unsigned char' values, not an array of 'char' values.  See
dfa500
@@ -91,10 +82,10 @@ STRCASESTR (const char *haystack_start, const char *needle_start)
dfa500
   if (needle_len < LONG_NEEDLE_THRESHOLD)
dfa500
     return two_way_short_needle ((const unsigned char *) haystack,
dfa500
 				 haystack_len,
dfa500
-				 (const unsigned char *) needle_start,
dfa500
+				 (const unsigned char *) needle,
dfa500
 				 needle_len);
dfa500
   return two_way_long_needle ((const unsigned char *) haystack, haystack_len,
dfa500
-			      (const unsigned char *) needle_start,
dfa500
+			      (const unsigned char *) needle,
dfa500
 			      needle_len);
dfa500
 }
dfa500
 
dfa500
diff --git a/string/strstr.c b/string/strstr.c
dfa500
index 79ebcc75329d0b17..f74d7189ed1319f6 100644
dfa500
--- a/string/strstr.c
dfa500
+++ b/string/strstr.c
dfa500
@@ -51,33 +51,32 @@
dfa500
    if NEEDLE is empty, otherwise NULL if NEEDLE is not found in
dfa500
    HAYSTACK.  */
dfa500
 char *
dfa500
-STRSTR (const char *haystack_start, const char *needle_start)
dfa500
+STRSTR (const char *haystack, const char *needle)
dfa500
 {
dfa500
-  const char *haystack = haystack_start;
dfa500
-  const char *needle = needle_start;
dfa500
   size_t needle_len; /* Length of NEEDLE.  */
dfa500
   size_t haystack_len; /* Known minimum length of HAYSTACK.  */
dfa500
-  bool ok = true; /* True if NEEDLE is prefix of HAYSTACK.  */
dfa500
-
dfa500
-  /* Determine length of NEEDLE, and in the process, make sure
dfa500
-     HAYSTACK is at least as long (no point processing all of a long
dfa500
-     NEEDLE if HAYSTACK is too short).  */
dfa500
-  while (*haystack && *needle)
dfa500
-    ok &= *haystack++ == *needle++;
dfa500
-  if (*needle)
dfa500
+
dfa500
+  /* Handle empty NEEDLE special case.  */
dfa500
+  if (needle[0] == '\0')
dfa500
+    return (char *) haystack;
dfa500
+
dfa500
+  /* Skip until we find the first matching char from NEEDLE.  */
dfa500
+  haystack = strchr (haystack, needle[0]);
dfa500
+  if (haystack == NULL || needle[1] == '\0')
dfa500
+    return (char *) haystack;
dfa500
+
dfa500
+  /* Ensure HAYSTACK length is at least as long as NEEDLE length.
dfa500
+     Since a match may occur early on in a huge HAYSTACK, use strnlen
dfa500
+     and read ahead a few cachelines for improved performance.  */
dfa500
+  needle_len = strlen (needle);
dfa500
+  haystack_len = __strnlen (haystack, needle_len + 256);
dfa500
+  if (haystack_len < needle_len)
dfa500
     return NULL;
dfa500
-  if (ok)
dfa500
-    return (char *) haystack_start;
dfa500
-
dfa500
-  /* Reduce the size of haystack using strchr, since it has a smaller
dfa500
-     linear coefficient than the Two-Way algorithm.  */
dfa500
-  needle_len = needle - needle_start;
dfa500
-  haystack = strchr (haystack_start + 1, *needle_start);
dfa500
-  if (!haystack || __builtin_expect (needle_len == 1, 0))
dfa500
+
dfa500
+  /* Check whether we have a match.  This improves performance since we avoid
dfa500
+     the initialization overhead of the two-way algorithm.  */
dfa500
+  if (memcmp (haystack, needle, needle_len) == 0)
dfa500
     return (char *) haystack;
dfa500
-  needle -= needle_len;
dfa500
-  haystack_len = (haystack > haystack_start + needle_len ? 1
dfa500
-		  : needle_len + haystack_start - haystack);
dfa500
 
dfa500
   /* Perform the search.  Abstract memory is considered to be an array
dfa500
      of 'unsigned char' values, not an array of 'char' values.  See