[clang] [Serialization] Avoid repeated hash lookups (NFC) (PR #110950)

Kazu Hirata via cfe-commits cfe-commits at lists.llvm.org
Wed Oct 2 20:14:24 PDT 2024


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

None

>From 9be1bde51a7b095b285bdfb81123749b6364d524 Mon Sep 17 00:00:00 2001
From: Kazu Hirata <kazu at google.com>
Date: Wed, 2 Oct 2024 07:02:53 -0700
Subject: [PATCH] [Serialization] Avoid repeated hash lookups (NFC)

---
 clang/lib/Serialization/GlobalModuleIndex.cpp | 15 +++++++--------
 1 file changed, 7 insertions(+), 8 deletions(-)

diff --git a/clang/lib/Serialization/GlobalModuleIndex.cpp b/clang/lib/Serialization/GlobalModuleIndex.cpp
index 1163943c5dffaa..9c48712a0b3fbe 100644
--- a/clang/lib/Serialization/GlobalModuleIndex.cpp
+++ b/clang/lib/Serialization/GlobalModuleIndex.cpp
@@ -430,14 +430,13 @@ namespace {
 
     /// Retrieve the module file information for the given file.
     ModuleFileInfo &getModuleFileInfo(FileEntryRef File) {
-      auto Known = ModuleFiles.find(File);
-      if (Known != ModuleFiles.end())
-        return Known->second;
-
-      unsigned NewID = ModuleFiles.size();
-      ModuleFileInfo &Info = ModuleFiles[File];
-      Info.ID = NewID;
-      return Info;
+      auto [It, Inserted] = ModuleFiles.try_emplace(File);
+      if (Inserted) {
+        unsigned NewID = ModuleFiles.size();
+        ModuleFileInfo &Info = It->second;
+        Info.ID = NewID;
+      }
+      return It->second;
     }
 
   public:



More information about the cfe-commits mailing list