[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
This commit is contained in:
parent
350d686444
commit
826f385348
|
@ -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;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue