Skip to content

[TableGen] Avoid repeated map lookups (NFC) #123699

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversation

kazutakahirata
Copy link
Contributor

No description provided.

@llvmbot
Copy link
Member

llvmbot commented Jan 21, 2025

@llvm/pr-subscribers-tablegen

Author: Kazu Hirata (kazutakahirata)

Changes

Full diff: https://github.com/llvm/llvm-project/pull/123699.diff

1 Files Affected:

  • (modified) llvm/utils/TableGen/Basic/VTEmitter.cpp (+7-6)
diff --git a/llvm/utils/TableGen/Basic/VTEmitter.cpp b/llvm/utils/TableGen/Basic/VTEmitter.cpp
index d02932dd5e7fca..07840d397bb156 100644
--- a/llvm/utils/TableGen/Basic/VTEmitter.cpp
+++ b/llvm/utils/TableGen/Basic/VTEmitter.cpp
@@ -109,12 +109,13 @@ void VTEmitter::run(raw_ostream &OS) {
   auto UpdateVTRange = [&VTRanges](const char *Key, StringRef Name,
                                    bool Valid) {
     if (Valid) {
-      if (!VTRanges.count(Key))
-        VTRanges[Key].First = Name;
-      assert(!VTRanges[Key].Closed && "Gap detected!");
-      VTRanges[Key].Last = Name;
-    } else if (VTRanges.count(Key)) {
-      VTRanges[Key].Closed = true;
+      auto [It, Inserted] = VTRanges.try_emplace(Key);
+      if (Inserted)
+        It->second.First = Name;
+      assert(!It->second.Closed && "Gap detected!");
+      It->second.Last = Name;
+    } else if (auto It = VTRanges.find(Key); It != VTRanges.end()) {
+      It->second.Closed = true;
     }
   };
 

Copy link
Collaborator

@topperc topperc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@hstk30-hw hstk30-hw merged commit 1714fac into llvm:main Jan 21, 2025
10 checks passed
@kazutakahirata kazutakahirata deleted the cleanup_001_repeated_hash_lookups_llvm_TableGen branch January 21, 2025 08:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants