[clang] [ByteCode] Avoid repeated hash lookups (NFC) (PR #111273)

Kazu Hirata via cfe-commits cfe-commits at lists.llvm.org
Sat Oct 5 19:55:58 PDT 2024


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

None

>From 48c833ccac40522a563f6c1610eef409628a45d9 Mon Sep 17 00:00:00 2001
From: Kazu Hirata <kazu at google.com>
Date: Sat, 5 Oct 2024 10:22:58 -0700
Subject: [PATCH] [ByteCode] Avoid repeated hash lookups (NFC)

---
 clang/lib/AST/ByteCode/Program.cpp | 12 +++++-------
 1 file changed, 5 insertions(+), 7 deletions(-)

diff --git a/clang/lib/AST/ByteCode/Program.cpp b/clang/lib/AST/ByteCode/Program.cpp
index 969f523db51dfe..23245a66b578ae 100644
--- a/clang/lib/AST/ByteCode/Program.cpp
+++ b/clang/lib/AST/ByteCode/Program.cpp
@@ -284,15 +284,13 @@ Record *Program::getOrCreateRecord(const RecordDecl *RD) {
   if (!RD->isCompleteDefinition())
     return nullptr;
 
-  // Deduplicate records.
-  if (auto It = Records.find(RD); It != Records.end())
+  // Return an existing record if available.  Otherwise, we insert nullptr now
+  // and replace that later, so recursive calls to this function with the same
+  // RecordDecl don't run into infinite recursion.
+  auto [It, Inserted] = Records.try_emplace(RD);
+  if (!Inserted)
     return It->second;
 
-  // We insert nullptr now and replace that later, so recursive calls
-  // to this function with the same RecordDecl don't run into
-  // infinite recursion.
-  Records.insert({RD, nullptr});
-
   // Number of bytes required by fields and base classes.
   unsigned BaseSize = 0;
   // Number of bytes required by virtual base.



More information about the cfe-commits mailing list