- Notifications
You must be signed in to change notification settings - Fork14.5k
[llvm] Use *Map::try_emplace (NFC)#149257
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
kazutakahirata merged 1 commit intollvm:mainfromkazutakahirata:cleanup_20250716_try_emplace_llvmJul 17, 2025
Merged
[llvm] Use *Map::try_emplace (NFC)#149257
kazutakahirata merged 1 commit intollvm:mainfromkazutakahirata:cleanup_20250716_try_emplace_llvmJul 17, 2025
Uh oh!
There was an error while loading.Please reload this page.
Conversation
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
- 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 structuredbinding.
Member
llvmbot commentedJul 17, 2025 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
@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.hindex 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.cppindex 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 changesJul 17, 2025
dwblaikie approved these changesJul 17, 2025
5775851
intollvm:main 12 checks passed
Uh oh!
There was an error while loading.Please reload this page.
Sign up for freeto 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.