|
|
a2cf7d |
commit 5e0a7ecb6629461b28adc1a5aabcc0ede122f201
|
|
|
a2cf7d |
Author: Wilco Dijkstra <wdijkstr@arm.com>
|
|
|
a2cf7d |
Date: Wed Jun 12 11:38:52 2019 +0100
|
|
|
a2cf7d |
|
|
|
a2cf7d |
Improve performance of strstr
|
|
|
a2cf7d |
|
|
|
a2cf7d |
This patch significantly improves performance of strstr using a novel
|
|
|
a2cf7d |
modified Horspool algorithm. Needles up to size 256 use a bad-character
|
|
|
a2cf7d |
table indexed by hashed pairs of characters to quickly skip past mismatches.
|
|
|
a2cf7d |
Long needles use a self-adapting filtering step to avoid comparing the whole
|
|
|
a2cf7d |
needle repeatedly.
|
|
|
a2cf7d |
|
|
|
a2cf7d |
By limiting the needle length to 256, the shift table only requires 8 bits
|
|
|
a2cf7d |
per entry, lowering preprocessing overhead and minimizing cache effects.
|
|
|
a2cf7d |
This limit also implies worst-case performance is linear.
|
|
|
a2cf7d |
|
|
|
a2cf7d |
Small needles up to size 3 use a dedicated linear search. Very long needles
|
|
|
a2cf7d |
use the Two-Way algorithm.
|
|
|
a2cf7d |
|
|
|
a2cf7d |
The performance gain using the improved bench-strstr on Cortex-A72 is 5.8
|
|
|
a2cf7d |
times basic_strstr and 3.7 times twoway_strstr.
|
|
|
a2cf7d |
|
|
|
a2cf7d |
Tested against GLIBC testsuite, randomized tests and the GNULIB strstr test
|
|
|
a2cf7d |
(https://git.savannah.gnu.org/cgit/gnulib.git/tree/tests/test-strstr.c).
|
|
|
a2cf7d |
|
|
|
a2cf7d |
Reviewed-by: Szabolcs Nagy <szabolcs.nagy@arm.com>
|
|
|
a2cf7d |
|
|
|
a2cf7d |
* string/str-two-way.h (two_way_short_needle): Add inline to avoid
|
|
|
a2cf7d |
warning.
|
|
|
a2cf7d |
(two_way_long_needle): Block inlining.
|
|
|
a2cf7d |
* string/strstr.c (strstr2): Add new function.
|
|
|
a2cf7d |
(strstr3): Likewise.
|
|
|
a2cf7d |
(STRSTR): Completely rewrite strstr to improve performance.
|
|
|
a2cf7d |
|
|
|
a2cf7d |
diff --git a/string/str-two-way.h b/string/str-two-way.h
|
|
|
a2cf7d |
index 523d946c59412e1f..358959bef0fd6f74 100644
|
|
|
a2cf7d |
--- a/string/str-two-way.h
|
|
|
a2cf7d |
+++ b/string/str-two-way.h
|
|
|
a2cf7d |
@@ -221,7 +221,7 @@ critical_factorization (const unsigned char *needle, size_t needle_len,
|
|
|
a2cf7d |
most 2 * HAYSTACK_LEN - NEEDLE_LEN comparisons occur in searching.
|
|
|
a2cf7d |
If AVAILABLE modifies HAYSTACK_LEN (as in strstr), then at most 3 *
|
|
|
a2cf7d |
HAYSTACK_LEN - NEEDLE_LEN comparisons occur in searching. */
|
|
|
a2cf7d |
-static RETURN_TYPE
|
|
|
a2cf7d |
+static inline RETURN_TYPE
|
|
|
a2cf7d |
two_way_short_needle (const unsigned char *haystack, size_t haystack_len,
|
|
|
a2cf7d |
const unsigned char *needle, size_t needle_len)
|
|
|
a2cf7d |
{
|
|
|
a2cf7d |
@@ -382,8 +382,11 @@ two_way_short_needle (const unsigned char *haystack, size_t haystack_len,
|
|
|
a2cf7d |
and sublinear performance O(HAYSTACK_LEN / NEEDLE_LEN) is possible.
|
|
|
a2cf7d |
If AVAILABLE modifies HAYSTACK_LEN (as in strstr), then at most 3 *
|
|
|
a2cf7d |
HAYSTACK_LEN - NEEDLE_LEN comparisons occur in searching, and
|
|
|
a2cf7d |
- sublinear performance is not possible. */
|
|
|
a2cf7d |
-static RETURN_TYPE
|
|
|
a2cf7d |
+ sublinear performance is not possible.
|
|
|
a2cf7d |
+
|
|
|
a2cf7d |
+ Since this function is large and complex, block inlining to avoid
|
|
|
a2cf7d |
+ slowing down the common case of small needles. */
|
|
|
a2cf7d |
+__attribute__((noinline)) static RETURN_TYPE
|
|
|
a2cf7d |
two_way_long_needle (const unsigned char *haystack, size_t haystack_len,
|
|
|
a2cf7d |
const unsigned char *needle, size_t needle_len)
|
|
|
a2cf7d |
{
|
|
|
a2cf7d |
diff --git a/string/strstr.c b/string/strstr.c
|
|
|
a2cf7d |
index f74d7189ed1319f6..7ffb18ab4285b060 100644
|
|
|
a2cf7d |
--- a/string/strstr.c
|
|
|
a2cf7d |
+++ b/string/strstr.c
|
|
|
a2cf7d |
@@ -16,29 +16,17 @@
|
|
|
a2cf7d |
License along with the GNU C Library; if not, see
|
|
|
a2cf7d |
<http://www.gnu.org/licenses/>. */
|
|
|
a2cf7d |
|
|
|
a2cf7d |
-/* This particular implementation was written by Eric Blake, 2008. */
|
|
|
a2cf7d |
-
|
|
|
a2cf7d |
#ifndef _LIBC
|
|
|
a2cf7d |
# include <config.h>
|
|
|
a2cf7d |
#endif
|
|
|
a2cf7d |
|
|
|
a2cf7d |
-/* Specification of strstr. */
|
|
|
a2cf7d |
#include <string.h>
|
|
|
a2cf7d |
|
|
|
a2cf7d |
-#include <stdbool.h>
|
|
|
a2cf7d |
-
|
|
|
a2cf7d |
-#ifndef _LIBC
|
|
|
a2cf7d |
-# define __builtin_expect(expr, val) (expr)
|
|
|
a2cf7d |
-#endif
|
|
|
a2cf7d |
-
|
|
|
a2cf7d |
#define RETURN_TYPE char *
|
|
|
a2cf7d |
#define AVAILABLE(h, h_l, j, n_l) \
|
|
|
a2cf7d |
(((j) + (n_l) <= (h_l)) \
|
|
|
a2cf7d |
|| ((h_l) += __strnlen ((void*)((h) + (h_l)), (n_l) + 512), \
|
|
|
a2cf7d |
(j) + (n_l) <= (h_l)))
|
|
|
a2cf7d |
-#define CHECK_EOL (1)
|
|
|
a2cf7d |
-#define RET0_IF_0(a) if (!a) goto ret0
|
|
|
a2cf7d |
-#define FASTSEARCH(S,C,N) (void*) strchr ((void*)(S), (C))
|
|
|
a2cf7d |
#include "str-two-way.h"
|
|
|
a2cf7d |
|
|
|
a2cf7d |
#undef strstr
|
|
|
a2cf7d |
@@ -47,47 +35,128 @@
|
|
|
a2cf7d |
#define STRSTR strstr
|
|
|
a2cf7d |
#endif
|
|
|
a2cf7d |
|
|
|
a2cf7d |
-/* Return the first occurrence of NEEDLE in HAYSTACK. Return HAYSTACK
|
|
|
a2cf7d |
- if NEEDLE is empty, otherwise NULL if NEEDLE is not found in
|
|
|
a2cf7d |
- HAYSTACK. */
|
|
|
a2cf7d |
-char *
|
|
|
a2cf7d |
-STRSTR (const char *haystack, const char *needle)
|
|
|
a2cf7d |
+static inline char *
|
|
|
a2cf7d |
+strstr2 (const unsigned char *hs, const unsigned char *ne)
|
|
|
a2cf7d |
{
|
|
|
a2cf7d |
- size_t needle_len; /* Length of NEEDLE. */
|
|
|
a2cf7d |
- size_t haystack_len; /* Known minimum length of HAYSTACK. */
|
|
|
a2cf7d |
-
|
|
|
a2cf7d |
- /* Handle empty NEEDLE special case. */
|
|
|
a2cf7d |
- if (needle[0] == '\0')
|
|
|
a2cf7d |
- return (char *) haystack;
|
|
|
a2cf7d |
+ uint32_t h1 = (ne[0] << 16) | ne[1];
|
|
|
a2cf7d |
+ uint32_t h2 = 0;
|
|
|
a2cf7d |
+ for (int c = hs[0]; h1 != h2 && c != 0; c = *++hs)
|
|
|
a2cf7d |
+ h2 = (h2 << 16) | c;
|
|
|
a2cf7d |
+ return h1 == h2 ? (char *)hs - 2 : NULL;
|
|
|
a2cf7d |
+}
|
|
|
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 |
+static inline char *
|
|
|
a2cf7d |
+strstr3 (const unsigned char *hs, const unsigned char *ne)
|
|
|
a2cf7d |
+{
|
|
|
a2cf7d |
+ uint32_t h1 = ((uint32_t)ne[0] << 24) | (ne[1] << 16) | (ne[2] << 8);
|
|
|
a2cf7d |
+ uint32_t h2 = 0;
|
|
|
a2cf7d |
+ for (int c = hs[0]; h1 != h2 && c != 0; c = *++hs)
|
|
|
a2cf7d |
+ h2 = (h2 | c) << 8;
|
|
|
a2cf7d |
+ return h1 == h2 ? (char *)hs - 3 : NULL;
|
|
|
a2cf7d |
+}
|
|
|
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 |
+/* Hash character pairs so a small shift table can be used. All bits of
|
|
|
a2cf7d |
+ p[0] are included, but not all bits from p[-1]. So if two equal hashes
|
|
|
a2cf7d |
+ match on p[-1], p[0] matches too. Hash collisions are harmless and result
|
|
|
a2cf7d |
+ in smaller shifts. */
|
|
|
a2cf7d |
+#define hash2(p) (((size_t)(p)[0] - ((size_t)(p)[-1] << 3)) % sizeof (shift))
|
|
|
a2cf7d |
+
|
|
|
a2cf7d |
+/* Fast strstr algorithm with guaranteed linear-time performance.
|
|
|
a2cf7d |
+ Small needles up to size 3 use a dedicated linear search. Longer needles
|
|
|
a2cf7d |
+ up to size 256 use a novel modified Horspool algorithm. It hashes pairs
|
|
|
a2cf7d |
+ of characters to quickly skip past mismatches. The main search loop only
|
|
|
a2cf7d |
+ exits if the last 2 characters match, avoiding unnecessary calls to memcmp
|
|
|
a2cf7d |
+ and allowing for a larger skip if there is no match. A self-adapting
|
|
|
a2cf7d |
+ filtering check is used to quickly detect mismatches in long needles.
|
|
|
a2cf7d |
+ By limiting the needle length to 256, the shift table can be reduced to 8
|
|
|
a2cf7d |
+ bits per entry, lowering preprocessing overhead and minimizing cache effects.
|
|
|
a2cf7d |
+ The limit also implies worst-case performance is linear.
|
|
|
a2cf7d |
+ Needles larger than 256 characters use the linear-time Two-Way algorithm. */
|
|
|
a2cf7d |
+char *
|
|
|
a2cf7d |
+STRSTR (const char *haystack, const char *needle)
|
|
|
a2cf7d |
+{
|
|
|
a2cf7d |
+ const unsigned char *hs = (const unsigned char *) haystack;
|
|
|
a2cf7d |
+ const unsigned char *ne = (const unsigned char *) needle;
|
|
|
a2cf7d |
+
|
|
|
a2cf7d |
+ /* Handle short needle special cases first. */
|
|
|
a2cf7d |
+ if (ne[0] == '\0')
|
|
|
a2cf7d |
+ return (char *)hs;
|
|
|
a2cf7d |
+ hs = (const unsigned char *)strchr ((const char*)hs, ne[0]);
|
|
|
a2cf7d |
+ if (hs == NULL || ne[1] == '\0')
|
|
|
a2cf7d |
+ return (char*)hs;
|
|
|
a2cf7d |
+ if (ne[2] == '\0')
|
|
|
a2cf7d |
+ return strstr2 (hs, ne);
|
|
|
a2cf7d |
+ if (ne[3] == '\0')
|
|
|
a2cf7d |
+ return strstr3 (hs, ne);
|
|
|
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 |
+ size_t ne_len = strlen ((const char*)ne);
|
|
|
a2cf7d |
+ size_t hs_len = __strnlen ((const char*)hs, ne_len | 512);
|
|
|
a2cf7d |
+ if (hs_len < ne_len)
|
|
|
a2cf7d |
return NULL;
|
|
|
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 |
-
|
|
|
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 |
- ISO C 99 section 6.2.6.1. */
|
|
|
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, needle_len);
|
|
|
a2cf7d |
- return two_way_long_needle ((const unsigned char *) haystack, haystack_len,
|
|
|
a2cf7d |
- (const unsigned char *) needle, needle_len);
|
|
|
a2cf7d |
+ /* Check whether we have a match. This improves performance since we
|
|
|
a2cf7d |
+ avoid initialization overheads. */
|
|
|
a2cf7d |
+ if (memcmp (hs, ne, ne_len) == 0)
|
|
|
a2cf7d |
+ return (char *) hs;
|
|
|
a2cf7d |
+
|
|
|
a2cf7d |
+ /* Use Two-Way algorithm for very long needles. */
|
|
|
a2cf7d |
+ if (__glibc_unlikely (ne_len > 256))
|
|
|
a2cf7d |
+ return two_way_long_needle (hs, hs_len, ne, ne_len);
|
|
|
a2cf7d |
+
|
|
|
a2cf7d |
+ const unsigned char *end = hs + hs_len - ne_len;
|
|
|
a2cf7d |
+ uint8_t shift[256];
|
|
|
a2cf7d |
+ size_t tmp, shift1;
|
|
|
a2cf7d |
+ size_t m1 = ne_len - 1;
|
|
|
a2cf7d |
+ size_t offset = 0;
|
|
|
a2cf7d |
+
|
|
|
a2cf7d |
+ /* Initialize bad character shift hash table. */
|
|
|
a2cf7d |
+ memset (shift, 0, sizeof (shift));
|
|
|
a2cf7d |
+ for (int i = 1; i < m1; i++)
|
|
|
a2cf7d |
+ shift[hash2 (ne + i)] = i;
|
|
|
a2cf7d |
+ /* Shift1 is the amount we can skip after matching the hash of the
|
|
|
a2cf7d |
+ needle end but not the full needle. */
|
|
|
a2cf7d |
+ shift1 = m1 - shift[hash2 (ne + m1)];
|
|
|
a2cf7d |
+ shift[hash2 (ne + m1)] = m1;
|
|
|
a2cf7d |
+
|
|
|
a2cf7d |
+ while (1)
|
|
|
a2cf7d |
+ {
|
|
|
a2cf7d |
+ if (__glibc_unlikely (hs > end))
|
|
|
a2cf7d |
+ {
|
|
|
a2cf7d |
+ end += __strnlen ((const char*)end + m1 + 1, 2048);
|
|
|
a2cf7d |
+ if (hs > end)
|
|
|
a2cf7d |
+ return NULL;
|
|
|
a2cf7d |
+ }
|
|
|
a2cf7d |
+
|
|
|
a2cf7d |
+ /* Skip past character pairs not in the needle. */
|
|
|
a2cf7d |
+ do
|
|
|
a2cf7d |
+ {
|
|
|
a2cf7d |
+ hs += m1;
|
|
|
a2cf7d |
+ tmp = shift[hash2 (hs)];
|
|
|
a2cf7d |
+ }
|
|
|
a2cf7d |
+ while (tmp == 0 && hs <= end);
|
|
|
a2cf7d |
+
|
|
|
a2cf7d |
+ /* If the match is not at the end of the needle, shift to the end
|
|
|
a2cf7d |
+ and continue until we match the hash of the needle end. */
|
|
|
a2cf7d |
+ hs -= tmp;
|
|
|
a2cf7d |
+ if (tmp < m1)
|
|
|
a2cf7d |
+ continue;
|
|
|
a2cf7d |
+
|
|
|
a2cf7d |
+ /* Hash of the last 2 characters matches. If the needle is long,
|
|
|
a2cf7d |
+ try to quickly filter out mismatches. */
|
|
|
a2cf7d |
+ if (m1 < 15 || memcmp (hs + offset, ne + offset, 8) == 0)
|
|
|
a2cf7d |
+ {
|
|
|
a2cf7d |
+ if (memcmp (hs, ne, m1) == 0)
|
|
|
a2cf7d |
+ return (void *) hs;
|
|
|
a2cf7d |
+
|
|
|
a2cf7d |
+ /* Adjust filter offset when it doesn't find the mismatch. */
|
|
|
a2cf7d |
+ offset = (offset >= 8 ? offset : m1) - 8;
|
|
|
a2cf7d |
+ }
|
|
|
a2cf7d |
+
|
|
|
a2cf7d |
+ /* Skip based on matching the hash of the needle end. */
|
|
|
a2cf7d |
+ hs += shift1;
|
|
|
a2cf7d |
+ }
|
|
|
a2cf7d |
}
|
|
|
a2cf7d |
libc_hidden_builtin_def (strstr)
|
|
|
a2cf7d |
-
|
|
|
a2cf7d |
-#undef LONG_NEEDLE_THRESHOLD
|