Skip to content

Commit 7016d43

Browse files
authored
[NFC][SpecialCaseList] Convert preprocess into LazyInit (#167281)
Currently SpecialCaseList created at least twice, one on by `Driver`, for diagnostics only, and then the real one by the `ASTContext`. Also, deppending on enabled sanitizers, not all sections will be used. In both cases there is unnecessary RadixTree construction. This patch changes `GlobMatcher` to do initialization lazily only when needed. And remove empty one from `RegexMatcher`. This saves saves 0.5% of clang time building large project.
1 parent 1f3e2c6 commit 7016d43

File tree

1 file changed

+18
-27
lines changed

1 file changed

+18
-27
lines changed

llvm/lib/Support/SpecialCaseList.cpp

Lines changed: 18 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "llvm/ADT/StringMap.h"
2121
#include "llvm/ADT/StringRef.h"
2222
#include "llvm/ADT/iterator_range.h"
23+
#include "llvm/Support/Compiler.h"
2324
#include "llvm/Support/GlobPattern.h"
2425
#include "llvm/Support/LineIterator.h"
2526
#include "llvm/Support/MemoryBuffer.h"
@@ -42,10 +43,9 @@ namespace {
4243
class RegexMatcher {
4344
public:
4445
Error insert(StringRef Pattern, unsigned LineNumber);
45-
void preprocess();
46-
4746
unsigned match(StringRef Query) const;
4847

48+
private:
4949
struct Reg {
5050
Reg(StringRef Name, unsigned LineNo, Regex &&Rg)
5151
: Name(Name), LineNo(LineNo), Rg(std::move(Rg)) {}
@@ -60,10 +60,9 @@ class RegexMatcher {
6060
class GlobMatcher {
6161
public:
6262
Error insert(StringRef Pattern, unsigned LineNumber);
63-
void preprocess();
64-
6563
unsigned match(StringRef Query) const;
6664

65+
private:
6766
struct Glob {
6867
Glob(StringRef Name, unsigned LineNo, GlobPattern &&Pattern)
6968
: Name(Name), LineNo(LineNo), Pattern(std::move(Pattern)) {}
@@ -72,15 +71,20 @@ class GlobMatcher {
7271
GlobPattern Pattern;
7372
};
7473

74+
void LazyInit() const;
75+
7576
std::vector<GlobMatcher::Glob> Globs;
7677

77-
RadixTree<iterator_range<StringRef::const_iterator>,
78-
RadixTree<iterator_range<StringRef::const_reverse_iterator>,
79-
SmallVector<int, 1>>>
78+
mutable RadixTree<iterator_range<StringRef::const_iterator>,
79+
RadixTree<iterator_range<StringRef::const_reverse_iterator>,
80+
SmallVector<int, 1>>>
8081
PrefixSuffixToGlob;
8182

82-
RadixTree<iterator_range<StringRef::const_iterator>, SmallVector<int, 1>>
83+
mutable RadixTree<iterator_range<StringRef::const_iterator>,
84+
SmallVector<int, 1>>
8385
SubstrToGlob;
86+
87+
mutable bool Initialized = false;
8488
};
8589

8690
/// Represents a set of patterns and their line numbers
@@ -89,7 +93,6 @@ class Matcher {
8993
Matcher(bool UseGlobs, bool RemoveDotSlash);
9094

9195
Error insert(StringRef Pattern, unsigned LineNumber);
92-
void preprocess();
9396
unsigned match(StringRef Query) const;
9497

9598
bool matchAny(StringRef Query) const { return match(Query); }
@@ -122,8 +125,6 @@ Error RegexMatcher::insert(StringRef Pattern, unsigned LineNumber) {
122125
return Error::success();
123126
}
124127

125-
void RegexMatcher::preprocess() {}
126-
127128
unsigned RegexMatcher::match(StringRef Query) const {
128129
for (const auto &R : reverse(RegExes))
129130
if (R.Rg.match(Query))
@@ -142,7 +143,10 @@ Error GlobMatcher::insert(StringRef Pattern, unsigned LineNumber) {
142143
return Error::success();
143144
}
144145

145-
void GlobMatcher::preprocess() {
146+
void GlobMatcher::LazyInit() const {
147+
if (LLVM_LIKELY(Initialized))
148+
return;
149+
Initialized = true;
146150
for (const auto &[Idx, G] : enumerate(Globs)) {
147151
StringRef Prefix = G.Pattern.prefix();
148152
StringRef Suffix = G.Pattern.suffix();
@@ -167,6 +171,8 @@ void GlobMatcher::preprocess() {
167171
}
168172

169173
unsigned GlobMatcher::match(StringRef Query) const {
174+
LazyInit();
175+
170176
int Best = -1;
171177
if (!PrefixSuffixToGlob.empty()) {
172178
for (const auto &[_, SToGlob] : PrefixSuffixToGlob.find_prefixes(Query)) {
@@ -224,10 +230,6 @@ Error Matcher::insert(StringRef Pattern, unsigned LineNumber) {
224230
return std::visit([&](auto &V) { return V.insert(Pattern, LineNumber); }, M);
225231
}
226232

227-
void Matcher::preprocess() {
228-
return std::visit([&](auto &V) { return V.preprocess(); }, M);
229-
}
230-
231233
unsigned Matcher::match(StringRef Query) const {
232234
if (RemoveDotSlash)
233235
Query = llvm::sys::path::remove_leading_dotslash(Query);
@@ -237,7 +239,6 @@ unsigned Matcher::match(StringRef Query) const {
237239

238240
class SpecialCaseList::Section::SectionImpl {
239241
public:
240-
void preprocess();
241242
const Matcher *findMatcher(StringRef Prefix, StringRef Category) const;
242243

243244
using SectionEntries = StringMap<StringMap<Matcher>>;
@@ -395,9 +396,6 @@ bool SpecialCaseList::parse(unsigned FileIdx, const MemoryBuffer *MB,
395396
}
396397
}
397398

398-
for (Section &S : Sections)
399-
S.Impl->preprocess();
400-
401399
return true;
402400
}
403401

@@ -448,13 +446,6 @@ SpecialCaseList::Section::SectionImpl::findMatcher(StringRef Prefix,
448446
return &II->second;
449447
}
450448

451-
void SpecialCaseList::Section::SectionImpl::preprocess() {
452-
SectionMatcher.preprocess();
453-
for (auto &[K1, E] : Entries)
454-
for (auto &[K2, M] : E)
455-
M.preprocess();
456-
}
457-
458449
unsigned SpecialCaseList::Section::getLastMatch(StringRef Prefix,
459450
StringRef Query,
460451
StringRef Category) const {

0 commit comments

Comments
 (0)