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