diff --git a/string/str-two-way.h b/string/str-two-way.h
index b5011baafa77a2d211598be246657b9a33fd8a2e..50fe6b46848bb1e758a667957ab06cfa207bcacb 100644
--- a/string/str-two-way.h
+++ b/string/str-two-way.h
@@ -221,7 +221,7 @@ critical_factorization (const unsigned char *needle, size_t needle_len,
Â Â Â  most 2 * HAYSTACK_LEN - NEEDLE_LEN comparisons occur in searching.
Â Â Â  If AVAILABLE modifies HAYSTACK_LEN (as in strstr), then at most 3 *
Â Â Â  HAYSTACK_LEN - NEEDLE_LEN comparisons occur in searching.Â  */
-static RETURN_TYPE
+static inline RETURN_TYPE
Â two_way_short_needle (const unsigned char *haystack, size_t haystack_len,
Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â  const unsigned char *needle, size_t needle_len)
Â {
@@ -383,7 +383,7 @@ two_way_short_needle (const unsigned char *haystack, size_t haystack_len,
Â Â Â  If AVAILABLE modifies HAYSTACK_LEN (as in strstr), then at most 3 *
Â Â Â  HAYSTACK_LEN - NEEDLE_LEN comparisons occur in searching, and
Â Â Â  sublinear performance is not possible.Â  */
-static RETURN_TYPE
+__attribute__((noinline)) static RETURN_TYPE
Â two_way_long_needle (const unsigned char *haystack, size_t haystack_len,
Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â  const unsigned char *needle, size_t needle_len)
Â {
diff --git a/string/strstr.c b/string/strstr.c
index 64e478b9e765a903228dca0bd1504096b374f12c..e2333025fea01d4dea7b586ab3f2dba02c8a224e 100644
--- a/string/strstr.c
+++ b/string/strstr.c
@@ -16,29 +16,17 @@
Â Â Â  License along with the GNU C Library; if not, see
Â Â Â  <http://www.gnu.org/licenses/>.Â  */
Â 
-/* This particular implementation was written by Eric Blake, 2008.Â  */
-
Â #ifndef _LIBC
Â # include <config.h>
Â #endif
Â 
-/* Specification of strstr.Â  */
Â #include <string.h>
Â 
-#include <stdbool.h>
-
-#ifndef _LIBC
-# define __builtin_expect(expr, val)Â Â  (expr)
-#endif
-
Â #define RETURN_TYPE char *
Â #define AVAILABLE(h, h_l, j, n_l)Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â  \
Â Â  (((j) + (n_l) <= (h_l)) \
Â Â Â  || ((h_l) += __strnlen ((void*)((h) + (h_l)), (n_l) + 512), \
Â Â Â Â Â Â Â  (j) + (n_l) <= (h_l)))
-#define CHECK_EOL (1)
-#define RET0_IF_0(a) if (!a) goto ret0
-#define FASTSEARCH(S,C,N) (void*) strchr ((void*)(S), (C))
Â #include "str-two-way.h"
Â 
Â #undef strstr
@@ -47,47 +35,122 @@
Â #define STRSTR strstr
Â #endif
Â 
-/* Return the first occurrence of NEEDLE in HAYSTACK.Â  Return HAYSTACK
-Â Â  if NEEDLE is empty, otherwise NULL if NEEDLE is not found in
-Â Â  HAYSTACK.Â  */
-char *
-STRSTR (const char *haystack, const char *needle)
+static inline char *
+strstr2 (const unsigned char *hs, const unsigned char *ne)
Â {
-Â  size_t needle_len; /* Length of NEEDLE.Â  */
-Â  size_t haystack_len; /* Known minimum length of HAYSTACK.Â  */
-
-Â  /* Handle empty NEEDLE special case.Â  */
-Â  if (needle[0] == '\0')
-Â Â Â  return (char *) haystack;
+Â  uint32_t h1 = (ne[0] << 16) | ne[1];
+Â  uint32_t h2 = 0;
+Â  for (int c = hs[0]; h1 != h2 && c != 0; c = *++hs)
+Â Â Â Â Â  h2 = (h2 << 16) | c;
+Â  return h1 == h2 ? (char *)hs - 2 : NULL;
+}
Â 
-Â  /* Skip until we find the first matching char from NEEDLE.Â  */
-Â  haystack = strchr (haystack, needle[0]);
-Â  if (haystack == NULL || needle[1] == '\0')
-Â Â Â  return (char *) haystack;
+static inline char *
+strstr3 (const unsigned char *hs, const unsigned char *ne)
+{
+Â  uint32_t h1 = (ne[0] << 24) | (ne[1] << 16) | (ne[2] << 8);
+Â  uint32_t h2 = 0;
+Â  for (int c = hs[0]; h1 != h2 && c != 0; c = *++hs)
+Â Â Â Â Â  h2 = (h2 | c) << 8;
+Â  return h1 == h2 ? (char *)hs - 3 : NULL;
+}
Â 
-Â  /* Ensure HAYSTACK length is at least as long as NEEDLE length.
-Â Â Â Â  Since a match may occur early on in a huge HAYSTACK, use strnlen
+#define hash2(p) (((size_t)(p)[0] - ((size_t)(p)[-1] << 3)) % sizeof (shift))
+
+/* Fast strstr algorithm with guaranteed linear-time performance.
+Â Â  Small needles up to size 2 use a dedicated linear search.Â  Longer needles
+Â Â  up to size 256 use a novel modified Horspool algorithm.Â  It hashes pairs
+Â Â  of characters to quickly skip past mismatches.Â  The main search loop only
+Â Â  exits if the last 2 characters match, avoiding unnecessary calls to memcmp
+Â Â  and allowing for a larger skip if there is no match.Â  A self-adapting
+Â Â  filtering check is used to quickly detect mismatches in long needles.
+Â Â  By limiting the needle length to 256, the shift table can be reduced to 8
+Â Â  bits per entry, lowering preprocessing overhead and minimizing cache effects.
+Â Â  The limit also implies worst-case performance is linear.
+Â Â  Needles larger than 256 characters use the linear-time Two-Way algorithm.Â  */
+char *
+STRSTR (const char *haystack, const char *needle)
+{
+Â  const unsigned char *hs = (const unsigned char *) haystack;
+Â  const unsigned char *ne = (const unsigned char *) needle;
+
+Â  /* Handle short needle special cases first.Â  */
+Â  if (ne[0] == '\0')
+Â Â Â  return (char *)hs;
+Â  hs = (const unsigned char *)strchr ((const char*)hs, ne[0]);
+Â  if (hs == NULL || ne[1] == '\0')
+Â Â Â  return (char*)hs;
+Â  if (ne[2] == '\0')
+Â Â Â  return strstr2 (hs, ne);
+Â  if (ne[3] == '\0')
+Â Â Â  return strstr3 (hs, ne);
+
+Â  /* Ensure haystack length is at least as long as needle length.
+Â Â Â Â  Since a match may occur early on in a huge haystack, use strnlen
Â Â Â Â Â  and read ahead a few cachelines for improved performance.Â  */
-Â  needle_len = strlen (needle);
-Â  haystack_len = __strnlen (haystack, needle_len + 256);
-Â  if (haystack_len < needle_len)
+Â  size_t ne_len = strlen ((const char*)ne);
+Â  size_t hs_len = __strnlen ((const char*)hs, ne_len | 512);
+Â  if (hs_len < ne_len)
Â Â Â Â  return NULL;
Â 
-Â  /* Check whether we have a match.Â  This improves performance since we avoid
-Â Â Â Â  the initialization overhead of the two-way algorithm.Â  */
-Â  if (memcmp (haystack, needle, needle_len) == 0)
-Â Â Â  return (char *) haystack;
-
-Â  /* Perform the search.Â  Abstract memory is considered to be an array
-Â Â Â Â  of 'unsigned char' values, not an array of 'char' values.Â  See
-Â Â Â Â  ISO C 99 section 6.2.6.1.Â  */
-Â  if (needle_len < LONG_NEEDLE_THRESHOLD)
-Â Â Â  return two_way_short_needle ((const unsigned char *) haystack,
-Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â  haystack_len,
-Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â  (const unsigned char *) needle, needle_len);
-Â  return two_way_long_needle ((const unsigned char *) haystack, haystack_len,
-Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â  (const unsigned char *) needle, needle_len);
+Â  /* Check whether we have a match.Â  This improves performance since we
+Â Â Â Â  avoid initialization overheads.Â  */
+Â  if (memcmp (hs, ne, ne_len) == 0)
+Â Â Â  return (char *) hs;
+
+Â  /* Use Two-Way algorithm for very long needles.Â  */
+Â  if (__glibc_unlikely (ne_len > 256))
+Â Â Â  return two_way_long_needle (hs, hs_len, ne, ne_len);
+
+Â  const unsigned char *end = hs + hs_len - ne_len;
+Â  uint8_t shift[256];
+Â  size_t tmp, shift1;
+Â  size_t m1 = ne_len - 1;
+Â  size_t offset = 0;
+
+Â  /* Initialize bad character shift hash table.Â  */
+Â  memset (shift, 0, sizeof (shift));
+Â  for (int i = 1; i < m1; i++)
+Â Â Â  shift[hash2 (ne + i)] = i;
+Â  shift1 = m1 - shift[hash2 (ne + m1)];
+Â  shift[hash2 (ne + m1)] = m1;
+
+Â  while (1)
+Â Â Â  {
+Â Â Â Â Â  if (__glibc_unlikely (hs > end))
+Â Â Â Â Â Â  {
+Â Â Â Â Â Â Â Â  end += __strnlen ((const char*)end + m1 + 1, 2048);
+Â Â Â Â Â Â Â Â  if (hs > end)
+Â Â Â Â Â Â Â Â Â Â  return NULL;
+Â Â Â Â Â Â  }
+
+Â Â Â Â Â  /* Skip past character pairs not in the needle.Â  */
+Â Â Â Â Â  do
+Â Â Â Â Â Â  {
+Â Â Â Â Â Â Â Â  hs += m1;
+Â Â Â Â Â Â Â Â  tmp = shift[hash2 (hs)];
+Â Â Â Â Â Â  }
+Â Â Â Â Â  while (hs <= end && tmp == 0);
+
+Â Â Â Â Â  /* If the match is not at the end of the needle, shift to the end
+Â Â Â Â Â Â Â  and continue until we match the last 2 characters.Â  */
+Â Â Â Â Â  hs -= tmp;
+Â Â Â Â Â  if (tmp < m1)
+Â Â Â Â Â Â  continue;
+
+Â Â Â Â Â  /* The last 2 characters match.Â  If the needle is long, check a
+Â Â Â Â Â Â Â  fixed number of characters first to quickly filter out mismatches.Â  */
+Â Â Â Â Â  if (m1 < 15 || memcmp (hs + offset, ne + offset, 8) == 0)
+Â Â Â Â Â Â  {
+Â Â Â Â Â Â Â Â  if (memcmp (hs, ne, m1) == 0)
+Â Â Â Â Â Â Â Â Â Â  return (void *) hs;
+
+Â Â Â Â Â Â Â Â  /* Adjust filter offset when it doesn't find the mismatch.Â  */
+Â Â Â Â Â Â Â Â  offset = (offset >= 8 ? offset : m1) - 8;
+Â Â Â Â Â Â  }
+
+Â Â Â Â Â  /* Skip based on matching the last 2 characters.Â  */
+Â Â Â Â Â  hs += shift1;
+Â Â Â  }
Â }
Â libc_hidden_builtin_def (strstr)
-
-#undef LONG_NEEDLE_THRESHOLD
