[llvm] [ProfileData] Use DenseMap::try_emplace (NFC) (PR #140394)

Kazu Hirata via llvm-commits llvm-commits at lists.llvm.org
Sat May 17 11:46:33 PDT 2025


https://github.com/kazutakahirata created https://github.com/llvm/llvm-project/pull/140394

We can simplify the code with structured binding and try_emplace.
Note that try_emplace default-constructs the value if omitted.

FWIW, structured binding, a C++17 feature, wasn't available in our
codebase at the time the code was written.


>From abb3a913fa8ab3950a50e0a45192eb927b21d564 Mon Sep 17 00:00:00 2001
From: Kazu Hirata <kazu at google.com>
Date: Sat, 17 May 2025 11:10:43 -0700
Subject: [PATCH] [ProfileData] Use DenseMap::try_emplace (NFC)

We can simplify the code with structured binding and try_emplace.
Note that try_emplace default-constructs the value if omitted.

FWIW, structured binding, a C++17 feature, wasn't available in our
codebase at the time the code was written.
---
 llvm/lib/ProfileData/InstrProfWriter.cpp | 10 ++--------
 1 file changed, 2 insertions(+), 8 deletions(-)

diff --git a/llvm/lib/ProfileData/InstrProfWriter.cpp b/llvm/lib/ProfileData/InstrProfWriter.cpp
index 9dc1a0d0b4678..39451c3d64870 100644
--- a/llvm/lib/ProfileData/InstrProfWriter.cpp
+++ b/llvm/lib/ProfileData/InstrProfWriter.cpp
@@ -178,10 +178,7 @@ void InstrProfWriter::overlapRecord(NamedInstrProfRecord &&Other,
     return;
   }
   auto &ProfileDataMap = It->second;
-  bool NewFunc;
-  ProfilingData::iterator Where;
-  std::tie(Where, NewFunc) =
-      ProfileDataMap.insert(std::make_pair(Hash, InstrProfRecord()));
+  auto [Where, NewFunc] = ProfileDataMap.try_emplace(Hash);
   if (NewFunc) {
     Overlap.addOneMismatch(FuncLevelOverlap.Test);
     return;
@@ -200,10 +197,7 @@ void InstrProfWriter::addRecord(StringRef Name, uint64_t Hash,
                                 function_ref<void(Error)> Warn) {
   auto &ProfileDataMap = FunctionData[Name];
 
-  bool NewFunc;
-  ProfilingData::iterator Where;
-  std::tie(Where, NewFunc) =
-      ProfileDataMap.insert(std::make_pair(Hash, InstrProfRecord()));
+  auto [Where, NewFunc] = ProfileDataMap.try_emplace(Hash);
   InstrProfRecord &Dest = Where->second;
 
   auto MapWarn = [&](instrprof_error E) {



More information about the llvm-commits mailing list