[clang] [Basic] Avoid repeated hash lookups (NFC) (PR #111228)
Kazu Hirata via cfe-commits
cfe-commits at lists.llvm.org
Sat Oct 5 09:57:39 PDT 2024
https://github.com/kazutakahirata updated https://github.com/llvm/llvm-project/pull/111228
>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 1/2] [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;
>From 53778b2f63ed276bf66073ebbcbeb3d8a61bfa22 Mon Sep 17 00:00:00 2001
From: Kazu Hirata <kazu at google.com>
Date: Sat, 5 Oct 2024 09:57:16 -0700
Subject: [PATCH 2/2] Remove an unused variable.
---
clang/lib/Basic/TargetID.cpp | 3 +--
1 file changed, 1 insertion(+), 2 deletions(-)
diff --git a/clang/lib/Basic/TargetID.cpp b/clang/lib/Basic/TargetID.cpp
index 18f649c7137d7d..b42d1f07013c26 100644
--- a/clang/lib/Basic/TargetID.cpp
+++ b/clang/lib/Basic/TargetID.cpp
@@ -92,9 +92,8 @@ parseTargetIDWithFormatCheckingOnly(llvm::StringRef TargetID,
if (Sign != '+' && Sign != '-')
return std::nullopt;
bool IsOn = Sign == '+';
- auto [Loc, Inserted] = FeatureMap->try_emplace(Feature, IsOn);
// Each feature can only show up at most once in target ID.
- if (!Inserted)
+ if (!FeatureMap->try_emplace(Feature, IsOn).second)
return std::nullopt;
Features = Splits.second;
}
More information about the cfe-commits
mailing list