diff options
author | Tatsuyuki Ishi <ishitatsuyuki@gmail.com> | 2022-10-27 10:14:09 +0200 |
---|---|---|
committer | Nikita Popov <npopov@redhat.com> | 2022-10-27 10:22:25 +0200 |
commit | 826f38534816ff2890639e3a75d9d7381ffde5ac (patch) | |
tree | 1a1422962c5c8e7caf898f549f96d1d511fc3569 /llvm/lib/Support/StringRef.cpp | |
parent | 350d68644445f53551df1a4ddd69bd4f54f09fff (diff) | |
download | llvm-826f38534816ff2890639e3a75d9d7381ffde5ac.zip llvm-826f38534816ff2890639e3a75d9d7381ffde5ac.tar.gz llvm-826f38534816ff2890639e3a75d9d7381ffde5ac.tar.bz2 |
[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
Diffstat (limited to 'llvm/lib/Support/StringRef.cpp')
-rw-r--r-- | llvm/lib/Support/StringRef.cpp | 16 |
1 files changed, 8 insertions, 8 deletions
diff --git a/llvm/lib/Support/StringRef.cpp b/llvm/lib/Support/StringRef.cpp index dd54d53..124df54 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; } |