Skip to content

Commit

Permalink
[ThinLTO] Avoid repeated std::map lookups (NFC) (#107156)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
kazutakahirata authored Sep 4, 2024
1 parent 771b7af commit 009184f
Showing 1 changed file with 5 additions and 6 deletions.
11 changes: 5 additions & 6 deletions llvm/include/llvm/IR/ModuleSummaryIndexYAML.h
Original file line number Diff line number Diff line change
Expand Up @@ -214,15 +214,13 @@ template <> struct CustomMappingTraits<GlobalValueSummaryMapTy> {
io.setError("key not an integer");
return;
}
if (!V.count(KeyInt))
V.emplace(KeyInt, /*IsAnalysis=*/false);
auto &Elem = V.find(KeyInt)->second;
auto &Elem = V.try_emplace(KeyInt, /*IsAnalysis=*/false).first->second;
for (auto &FSum : FSums) {
std::vector<ValueInfo> Refs;
Refs.reserve(FSum.Refs.size());
for (auto &RefGUID : FSum.Refs) {
if (!V.count(RefGUID))
V.emplace(RefGUID, /*IsAnalysis=*/false);
Refs.push_back(ValueInfo(/*IsAnalysis=*/false, &*V.find(RefGUID)));
auto It = V.try_emplace(RefGUID, /*IsAnalysis=*/false).first;
Refs.push_back(ValueInfo(/*IsAnalysis=*/false, &*It));
}
Elem.SummaryList.push_back(std::make_unique<FunctionSummary>(
GlobalValueSummary::GVFlags(
Expand All @@ -247,6 +245,7 @@ template <> struct CustomMappingTraits<GlobalValueSummaryMapTy> {
for (auto &Sum : P.second.SummaryList) {
if (auto *FSum = dyn_cast<FunctionSummary>(Sum.get())) {
std::vector<uint64_t> Refs;
Refs.reserve(FSum->refs().size());
for (auto &VI : FSum->refs())
Refs.push_back(VI.getGUID());
FSums.push_back(FunctionSummaryYaml{
Expand Down

0 comments on commit 009184f

Please sign in to comment.