Skip to content

Commit 009184f

Browse files
[ThinLTO] Avoid repeated std::map lookups (NFC) (llvm#107156)
This patch avoids repeated std::map lookups with try_emplace. While I am at it, this patch adds a couple of calls to std::vector::reserve.
1 parent 771b7af commit 009184f

File tree

1 file changed

+5
-6
lines changed

1 file changed

+5
-6
lines changed

llvm/include/llvm/IR/ModuleSummaryIndexYAML.h

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -214,15 +214,13 @@ template <> struct CustomMappingTraits<GlobalValueSummaryMapTy> {
214214
io.setError("key not an integer");
215215
return;
216216
}
217-
if (!V.count(KeyInt))
218-
V.emplace(KeyInt, /*IsAnalysis=*/false);
219-
auto &Elem = V.find(KeyInt)->second;
217+
auto &Elem = V.try_emplace(KeyInt, /*IsAnalysis=*/false).first->second;
220218
for (auto &FSum : FSums) {
221219
std::vector<ValueInfo> Refs;
220+
Refs.reserve(FSum.Refs.size());
222221
for (auto &RefGUID : FSum.Refs) {
223-
if (!V.count(RefGUID))
224-
V.emplace(RefGUID, /*IsAnalysis=*/false);
225-
Refs.push_back(ValueInfo(/*IsAnalysis=*/false, &*V.find(RefGUID)));
222+
auto It = V.try_emplace(RefGUID, /*IsAnalysis=*/false).first;
223+
Refs.push_back(ValueInfo(/*IsAnalysis=*/false, &*It));
226224
}
227225
Elem.SummaryList.push_back(std::make_unique<FunctionSummary>(
228226
GlobalValueSummary::GVFlags(
@@ -247,6 +245,7 @@ template <> struct CustomMappingTraits<GlobalValueSummaryMapTy> {
247245
for (auto &Sum : P.second.SummaryList) {
248246
if (auto *FSum = dyn_cast<FunctionSummary>(Sum.get())) {
249247
std::vector<uint64_t> Refs;
248+
Refs.reserve(FSum->refs().size());
250249
for (auto &VI : FSum->refs())
251250
Refs.push_back(VI.getGUID());
252251
FSums.push_back(FunctionSummaryYaml{

0 commit comments

Comments
 (0)