[llvm] Use *Map::try_emplace (NFC)#149257
Merged
kazutakahirata merged 1 commit intoJul 17, 2025
Merged
Conversation
- try_emplace(Key) is shorter than insert({Key, nullptr}).
- try_emplace performs value initialization without value parameters.
- We overwrite values on successful insertion anyway.
While we are at it, this patch simplifies the code with structured
binding.
Member
|
@llvm/pr-subscribers-llvm-adt @llvm/pr-subscribers-mc Author: Kazu Hirata (kazutakahirata) Changes
While we are at it, this patch simplifies the code with structured Full diff: https://github.com/llvm/llvm-project/pull/149257.diff 2 Files Affected:
diff --git a/llvm/include/llvm/ADT/EquivalenceClasses.h b/llvm/include/llvm/ADT/EquivalenceClasses.h
index b1009f8b49992..1a2331c1a0322 100644
--- a/llvm/include/llvm/ADT/EquivalenceClasses.h
+++ b/llvm/include/llvm/ADT/EquivalenceClasses.h
@@ -218,12 +218,12 @@ template <class ElemTy> class EquivalenceClasses {
/// insert - Insert a new value into the union/find set, ignoring the request
/// if the value already exists.
const ECValue &insert(const ElemTy &Data) {
- auto I = TheMapping.insert({Data, nullptr});
- if (!I.second)
- return *I.first->second;
+ auto [I, Inserted] = TheMapping.try_emplace(Data);
+ if (!Inserted)
+ return *I->second;
auto *ECV = new (ECValueAllocator) ECValue(Data);
- I.first->second = ECV;
+ I->second = ECV;
Members.push_back(ECV);
return *ECV;
}
diff --git a/llvm/lib/MC/MCContext.cpp b/llvm/lib/MC/MCContext.cpp
index 070be621a4b2c..12b3fbab8fb8f 100644
--- a/llvm/lib/MC/MCContext.cpp
+++ b/llvm/lib/MC/MCContext.cpp
@@ -734,9 +734,8 @@ MCSectionGOFF *MCContext::getGOFFSection(SectionKind Kind, StringRef Name,
UniqueName.append("/").append(P->getName());
}
// Do the lookup. If we don't have a hit, return a new section.
- auto IterBool = GOFFUniquingMap.insert(std::make_pair(UniqueName, nullptr));
- auto Iter = IterBool.first;
- if (!IterBool.second)
+ auto [Iter, Inserted] = GOFFUniquingMap.try_emplace(UniqueName);
+ if (!Inserted)
return Iter->second;
StringRef CachedName = StringRef(Iter->first.c_str(), Name.size());
|
arsenm
approved these changes
Jul 17, 2025
dwblaikie
approved these changes
Jul 17, 2025
This was referenced Jul 23, 2025
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
While we are at it, this patch simplifies the code with structured
binding.