[llvm] 5699692 - [Support] Add fast path for StringRef::find with needle of length 2.

David Blaikie via llvm-commits llvm-commits at lists.llvm.org
Sat Oct 8 17:58:30 PDT 2022


Author: Tatsuyuki Ishi
Date: 2022-10-09T00:58:07Z
New Revision: 5699692dfc7def5c0120258855e43b71e6d1b728

URL: https://github.com/llvm/llvm-project/commit/5699692dfc7def5c0120258855e43b71e6d1b728
DIFF: https://github.com/llvm/llvm-project/commit/5699692dfc7def5c0120258855e43b71e6d1b728.diff

LOG: [Support] Add fast path for StringRef::find with needle of length 2.

InclusionRewriter on Windows (CRLF line endings) will exercise this in a
hot path. Calling memcmp repeatedly would be highly suboptimal for that
use case, so give it a specialized path.

Reviewed By: efriedma

Differential Revision: https://reviews.llvm.org/D133660

Added: 
    

Modified: 
    llvm/lib/Support/StringRef.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Support/StringRef.cpp b/llvm/lib/Support/StringRef.cpp
index 096b2d2d8c078..7e19a79812305 100644
--- a/llvm/lib/Support/StringRef.cpp
+++ b/llvm/lib/Support/StringRef.cpp
@@ -148,6 +148,18 @@ size_t StringRef::find(StringRef Str, size_t From) const {
 
   const char *Stop = Start + (Size - N + 1);
 
+  if (N == 2) {
+    // Provide a fast path for newline finding (CRLF case) in InclusionRewriter.
+    // Not the most optimized strategy, but getting memcmp inlined should be
+    // good enough.
+    do {
+      if (std::memcmp(Start, Needle, 2) == 0)
+        return Start - Data;
+      ++Start;
+    } while (Start < Stop);
+    return npos;
+  }
+
   // For short haystacks or unsupported needles fall back to the naive algorithm
   if (Size < 16 || N > 255) {
     do {


        


More information about the llvm-commits mailing list