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