[clang-tools-extra] [modularize] Avoid repeated hash lookups (NFC) (PR #109508)

Kazu Hirata via cfe-commits cfe-commits at lists.llvm.org
Sat Sep 21 09:18:49 PDT 2024


https://github.com/kazutakahirata updated https://github.com/llvm/llvm-project/pull/109508

>From d50b829fd1277736b7e65886879fcfe9a45714f2 Mon Sep 17 00:00:00 2001
From: Kazu Hirata <kazu at google.com>
Date: Fri, 20 Sep 2024 13:31:12 -0700
Subject: [PATCH 1/3] [modularize] Avoid repeated hash lookups (NFC)

---
 clang-tools-extra/modularize/Modularize.cpp | 10 ++++------
 1 file changed, 4 insertions(+), 6 deletions(-)

diff --git a/clang-tools-extra/modularize/Modularize.cpp b/clang-tools-extra/modularize/Modularize.cpp
index f3e7dfc20b027d..92e4792526d6f3 100644
--- a/clang-tools-extra/modularize/Modularize.cpp
+++ b/clang-tools-extra/modularize/Modularize.cpp
@@ -508,13 +508,11 @@ class EntityMap : public std::map<std::string, SmallVector<Entry, 2>> {
       // Sort contents.
       llvm::sort(H->second);
 
-      // Check whether we've seen this header before.
-      auto KnownH = AllHeaderContents.find(H->first);
-      if (KnownH == AllHeaderContents.end()) {
-        // We haven't seen this header before; record its contents.
-        AllHeaderContents.insert(*H);
+      // Record this header and its contents if we haven't seen it before.
+      auto [KnownH, Inserted] =
+          AllHeaderContents.try_emplace(H->first, H->second);
+      if (Inserted)
         continue;
-      }
 
       // If the header contents are the same, we're done.
       if (H->second == KnownH->second)

>From 39dea55ef4af49f1d855d78a70a5b5f535e44405 Mon Sep 17 00:00:00 2001
From: Kazu Hirata <kazu at google.com>
Date: Sat, 21 Sep 2024 09:12:57 -0700
Subject: [PATCH 2/3] Update clang-tools-extra/modularize/Modularize.cpp

Co-authored-by: Nikita Popov <github at npopov.com>
---
 clang-tools-extra/modularize/Modularize.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/clang-tools-extra/modularize/Modularize.cpp b/clang-tools-extra/modularize/Modularize.cpp
index 92e4792526d6f3..9dda405a392bec 100644
--- a/clang-tools-extra/modularize/Modularize.cpp
+++ b/clang-tools-extra/modularize/Modularize.cpp
@@ -510,7 +510,7 @@ class EntityMap : public std::map<std::string, SmallVector<Entry, 2>> {
 
       // Record this header and its contents if we haven't seen it before.
       auto [KnownH, Inserted] =
-          AllHeaderContents.try_emplace(H->first, H->second);
+          AllHeaderContents.insert(*H);
       if (Inserted)
         continue;
 

>From 92292648faf08168d951e762ec191d1b21df2ce5 Mon Sep 17 00:00:00 2001
From: Kazu Hirata <kazu at google.com>
Date: Sat, 21 Sep 2024 09:18:17 -0700
Subject: [PATCH 3/3] Fix formatting.

---
 clang-tools-extra/modularize/Modularize.cpp | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/clang-tools-extra/modularize/Modularize.cpp b/clang-tools-extra/modularize/Modularize.cpp
index 9dda405a392bec..2c00c76c855336 100644
--- a/clang-tools-extra/modularize/Modularize.cpp
+++ b/clang-tools-extra/modularize/Modularize.cpp
@@ -509,8 +509,7 @@ class EntityMap : public std::map<std::string, SmallVector<Entry, 2>> {
       llvm::sort(H->second);
 
       // Record this header and its contents if we haven't seen it before.
-      auto [KnownH, Inserted] =
-          AllHeaderContents.insert(*H);
+      auto [KnownH, Inserted] = AllHeaderContents.insert(*H);
       if (Inserted)
         continue;
 



More information about the cfe-commits mailing list