Add a bad char heuristic to StringRef::find.
authorBenjamin Kramer <benny.kra@googlemail.com>
Sat, 15 Oct 2011 10:08:31 +0000 (10:08 +0000)
committerBenjamin Kramer <benny.kra@googlemail.com>
Sat, 15 Oct 2011 10:08:31 +0000 (10:08 +0000)
Based on Horspool's simplified version of Boyer-Moore. We use a constant-sized table of
uint8_ts to keep cache thrashing low, needles bigger than 255 bytes are uncommon anyways.

The worst case is still O(n*m) but we do a lot better on the average case now.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@142061 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Support/StringRef.cpp
unittests/ADT/StringRefTest.cpp

index b5b4f9476026ee9a4e48eb2d1dd7212ee2d2e2ee..a862ed2fa9ce6589cf987b972f730583072f77a6 100644 (file)
@@ -144,9 +144,32 @@ size_t StringRef::find(StringRef Str, size_t From) const {
   size_t N = Str.size();
   if (N > Length)
     return npos;
-  for (size_t e = Length - N + 1, i = min(From, e); i != e; ++i)
-    if (substr(i, N).equals(Str))
-      return i;
+
+  // For short haystacks or unsupported needles fall back to the naive algorithm
+  if (Length < 16 || N > 255 || N == 0) {
+    for (size_t e = Length - N + 1, i = min(From, e); i != e; ++i)
+      if (substr(i, N).equals(Str))
+        return i;
+    return npos;
+  }
+
+  // Build the bad char heuristic table, with uint8_t to reduce cache thrashing.
+  uint8_t BadCharSkip[256];
+  std::memset(BadCharSkip, N, 256);
+  for (unsigned i = 0; i != N-1; ++i)
+    BadCharSkip[(uint8_t)Str[i]] = N-1-i;
+
+  unsigned Len = Length, Pos = min(From, Length);
+  while (Len >= N) {
+    if (substr(Pos, N).equals(Str)) // See if this is the correct substring.
+      return Pos;
+
+    // Otherwise skip the appropriate number of bytes.
+    uint8_t Skip = BadCharSkip[(uint8_t)Data[Pos+N-1]];
+    Len -= Skip;
+    Pos += Skip;
+  }
+
   return npos;
 }
 
index 8364eac827487b70698370fac253f44904e80e37..d91084381ec304881cf59ac8fe89ed1d87644ef5 100644 (file)
@@ -245,6 +245,12 @@ TEST(StringRefTest, Find) {
   EXPECT_EQ(StringRef::npos, Str.find("zz"));
   EXPECT_EQ(2U, Str.find("ll", 2));
   EXPECT_EQ(StringRef::npos, Str.find("ll", 3));
+  EXPECT_EQ(0U, Str.find(""));
+  StringRef LongStr("hellx xello hell ello world foo bar hello");
+  EXPECT_EQ(36U, LongStr.find("hello"));
+  EXPECT_EQ(28U, LongStr.find("foo"));
+  EXPECT_EQ(12U, LongStr.find("hell", 2));
+  EXPECT_EQ(0U, LongStr.find(""));
 
   EXPECT_EQ(3U, Str.rfind('l'));
   EXPECT_EQ(StringRef::npos, Str.rfind('z'));