[llvm] 826f385 - [Support] Use find() for faster StringRef::count (NFC)
Nikita Popov via llvm-commits
llvm-commits at lists.llvm.org
Thu Oct 27 01:22:32 PDT 2022
Author: Tatsuyuki Ishi
Date: 2022-10-27T10:22:25+02:00
New Revision: 826f38534816ff2890639e3a75d9d7381ffde5ac
URL: https://github.com/llvm/llvm-project/commit/826f38534816ff2890639e3a75d9d7381ffde5ac
DIFF: https://github.com/llvm/llvm-project/commit/826f38534816ff2890639e3a75d9d7381ffde5ac.diff
LOG: [Support] Use find() for faster StringRef::count (NFC)
While profiling InclusionRewriter, it was found that counting lines was
so slow that it took up 20% of the processing time. Surely, calling
memcmp() of size 1 on every substring in the window isn't a good idea.
Use StringRef::find() instead; in the case of N=1 it will forward to
memcmp which is much more optimal. For 2<=N<256 it will run the same
memcmp loop as we have now, which is still suboptimal but at least does
not regress anything.
Differential Revision: https://reviews.llvm.org/D133658
Added:
Modified:
llvm/lib/Support/StringRef.cpp
Removed:
################################################################################
diff --git a/llvm/lib/Support/StringRef.cpp b/llvm/lib/Support/StringRef.cpp
index dd54d53861429..124df54c74d35 100644
--- a/llvm/lib/Support/StringRef.cpp
+++ b/llvm/lib/Support/StringRef.cpp
@@ -382,16 +382,16 @@ void StringRef::split(SmallVectorImpl<StringRef> &A, char Separator,
/// the string.
size_t StringRef::count(StringRef Str) const {
size_t Count = 0;
+ size_t Pos = 0;
size_t N = Str.size();
- if (!N || N > Length)
+ // TODO: For an empty `Str` we return 0 for legacy reasons. Consider changing
+ // this to `Length + 1` which is more in-line with the function
+ // description.
+ if (!N)
return 0;
- for (size_t i = 0, e = Length - N + 1; i < e;) {
- if (substr(i, N).equals(Str)) {
- ++Count;
- i += N;
- }
- else
- ++i;
+ while ((Pos = find(Str, Pos)) != npos) {
+ ++Count;
+ Pos += N;
}
return Count;
}
More information about the llvm-commits
mailing list