Skip to content

Commit 19cdab5

Browse files
committed
[Profiler] Avoid quadratic search for coverage filename in WMO
Previously we were doing a `std::find` over the files for each coverage mapping, which would be quadratic for WMO. Switch to using a DenseMap instead.
1 parent 570c364 commit 19cdab5

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

lib/IRGen/GenCoverage.cpp

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -84,15 +84,19 @@ void IRGenModule::emitCoverageMaps(ArrayRef<const SILCoverageMap *> Mappings) {
8484
llvm::getCoverageUnusedNamesVarName());
8585
}
8686

87-
std::vector<StringRef> Files;
87+
llvm::DenseMap<StringRef, unsigned> RawFileIndices;
88+
llvm::SmallVector<StringRef, 8> RawFiles;
8889
for (const auto &M : Mappings) {
89-
if (std::find(Files.begin(), Files.end(), M->getFilename()) == Files.end())
90-
Files.push_back(M->getFilename());
90+
auto Filename = M->getFilename();
91+
auto Inserted = RawFileIndices.insert({Filename, RawFiles.size()}).second;
92+
if (!Inserted)
93+
continue;
94+
RawFiles.push_back(Filename);
9195
}
9296
const auto &Remapper = getOptions().CoveragePrefixMap;
9397

9498
llvm::SmallVector<std::string, 8> FilenameStrs;
95-
FilenameStrs.reserve(Files.size() + 1);
99+
FilenameStrs.reserve(RawFiles.size() + 1);
96100

97101
// First element needs to be the current working directory. Note if this
98102
// scheme ever changes, the FileID computation below will need updating.
@@ -103,7 +107,7 @@ void IRGenModule::emitCoverageMaps(ArrayRef<const SILCoverageMap *> Mappings) {
103107
// Following elements are the filenames present. We use their relative path,
104108
// which llvm-cov will turn back into absolute paths using the working
105109
// directory element.
106-
for (auto Name : Files)
110+
for (auto Name : RawFiles)
107111
FilenameStrs.emplace_back(Remapper.remapPath(Name));
108112

109113
// Encode the filenames.
@@ -130,9 +134,11 @@ void IRGenModule::emitCoverageMaps(ArrayRef<const SILCoverageMap *> Mappings) {
130134

131135
// The file ID needs to be bumped by 1 to account for the working directory
132136
// as the first element.
133-
unsigned FileID = 1 +
134-
std::find(Files.begin(), Files.end(), M->getFilename()) -
135-
Files.begin();
137+
unsigned FileID = [&]() {
138+
auto Result = RawFileIndices.find(M->getFilename());
139+
assert(Result != RawFileIndices.end());
140+
return Result->second + 1;
141+
}();
136142
assert(FileID < FilenameStrs.size());
137143

138144
std::vector<CounterMappingRegion> Regions;

0 commit comments

Comments
 (0)