aboutsummaryrefslogtreecommitdiff
path: root/llvm/lib/Support/StringRef.cpp
diff options
context:
space:
mode:
authorTatsuyuki Ishi <ishitatsuyuki@gmail.com>2022-10-09 00:58:07 +0000
committerDavid Blaikie <dblaikie@gmail.com>2022-10-09 00:58:07 +0000
commit5699692dfc7def5c0120258855e43b71e6d1b728 (patch)
treea962e7f388e0b5346e4438482463c2b7aed3787c /llvm/lib/Support/StringRef.cpp
parent7b85e765000df36fcc6a5191dec9a28f444245ba (diff)
downloadllvm-5699692dfc7def5c0120258855e43b71e6d1b728.zip
llvm-5699692dfc7def5c0120258855e43b71e6d1b728.tar.gz
llvm-5699692dfc7def5c0120258855e43b71e6d1b728.tar.bz2
[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
Diffstat (limited to 'llvm/lib/Support/StringRef.cpp')
-rw-r--r--llvm/lib/Support/StringRef.cpp12
1 files changed, 12 insertions, 0 deletions
diff --git a/llvm/lib/Support/StringRef.cpp b/llvm/lib/Support/StringRef.cpp
index 096b2d2..7e19a79 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 {