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

Kazu Hirata via llvm-commits llvm-commits at lists.llvm.org
Mon Jan 20 23:02:36 PST 2025


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

None

>From 8d4ddb20f98584a432b5afa35f4b26ca8237e8cd Mon Sep 17 00:00:00 2001
From: Kazu Hirata <kazu at google.com>
Date: Mon, 20 Jan 2025 10:35:31 -0800
Subject: [PATCH] [TableGen] Avoid repeated map lookups (NFC)

---
 llvm/utils/TableGen/Basic/VTEmitter.cpp | 13 +++++++------
 1 file changed, 7 insertions(+), 6 deletions(-)

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;
     }
   };
 



More information about the llvm-commits mailing list