[clang] [Basic] Avoid repeated hash lookups (NFC) (PR #111228)

Kazu Hirata via cfe-commits cfe-commits at lists.llvm.org
Fri Oct 4 19:48:52 PDT 2024


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

None

>From 2e3d116613a7441b31036cf603d581db6e0afe9b Mon Sep 17 00:00:00 2001
From: Kazu Hirata <kazu at google.com>
Date: Fri, 4 Oct 2024 08:00:11 -0700
Subject: [PATCH] [Basic] Avoid repeated hash lookups (NFC)

---
 clang/lib/Basic/TargetID.cpp | 13 ++++++-------
 1 file changed, 6 insertions(+), 7 deletions(-)

diff --git a/clang/lib/Basic/TargetID.cpp b/clang/lib/Basic/TargetID.cpp
index fa1bfec2aacb9c..18f649c7137d7d 100644
--- a/clang/lib/Basic/TargetID.cpp
+++ b/clang/lib/Basic/TargetID.cpp
@@ -92,11 +92,10 @@ parseTargetIDWithFormatCheckingOnly(llvm::StringRef TargetID,
     if (Sign != '+' && Sign != '-')
       return std::nullopt;
     bool IsOn = Sign == '+';
-    auto Loc = FeatureMap->find(Feature);
+    auto [Loc, Inserted] = FeatureMap->try_emplace(Feature, IsOn);
     // Each feature can only show up at most once in target ID.
-    if (Loc != FeatureMap->end())
+    if (!Inserted)
       return std::nullopt;
-    (*FeatureMap)[Feature] = IsOn;
     Features = Splits.second;
   }
   return Processor;
@@ -147,15 +146,15 @@ getConflictTargetIDCombination(const std::set<llvm::StringRef> &TargetIDs) {
   struct Info {
     llvm::StringRef TargetID;
     llvm::StringMap<bool> Features;
+    Info(llvm::StringRef TargetID, const llvm::StringMap<bool> &Features)
+        : TargetID(TargetID), Features(Features) {}
   };
   llvm::StringMap<Info> FeatureMap;
   for (auto &&ID : TargetIDs) {
     llvm::StringMap<bool> Features;
     llvm::StringRef Proc = *parseTargetIDWithFormatCheckingOnly(ID, &Features);
-    auto Loc = FeatureMap.find(Proc);
-    if (Loc == FeatureMap.end())
-      FeatureMap[Proc] = Info{ID, Features};
-    else {
+    auto [Loc, Inserted] = FeatureMap.try_emplace(Proc, ID, Features);
+    if (!Inserted) {
       auto &ExistingFeatures = Loc->second.Features;
       if (llvm::any_of(Features, [&](auto &F) {
             return ExistingFeatures.count(F.first()) == 0;



More information about the cfe-commits mailing list