[llvm] [ThinLTO] Avoid repeated std::map lookups (NFC) (PR #107156)

Kazu Hirata via llvm-commits llvm-commits at lists.llvm.org
Tue Sep 3 14:54:55 PDT 2024


https://github.com/kazutakahirata created https://github.com/llvm/llvm-project/pull/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.


>From e6e49484056bfa246155b3ec8e6a5d1fd2cda611 Mon Sep 17 00:00:00 2001
From: Kazu Hirata <kazu at google.com>
Date: Tue, 3 Sep 2024 14:16:28 -0700
Subject: [PATCH] [ThinLTO] Avoid repeated std::map lookups (NFC)

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.
---
 llvm/include/llvm/IR/ModuleSummaryIndexYAML.h | 11 +++++------
 1 file changed, 5 insertions(+), 6 deletions(-)

diff --git a/llvm/include/llvm/IR/ModuleSummaryIndexYAML.h b/llvm/include/llvm/IR/ModuleSummaryIndexYAML.h
index b2747d24c5396d..6cc533f043a517 100644
--- a/llvm/include/llvm/IR/ModuleSummaryIndexYAML.h
+++ b/llvm/include/llvm/IR/ModuleSummaryIndexYAML.h
@@ -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(
@@ -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{



More information about the llvm-commits mailing list