[clang] [Sema] Avoid repeated hash lookups (NFC) (PR #109375)

Kazu Hirata via cfe-commits cfe-commits at lists.llvm.org
Thu Sep 19 22:10:03 PDT 2024


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

GlobalMethodPool, the type of MethodPool, is a type wrapping DenseMap
and exposes only a subset of the DenseMap methods.

This patch adds operator[] to GlobalMethodPool so that we can avoid
repeated hash lookups.  I don't bother using references or rvalue
references in operator[] because Selector, the key type, is small and
trivially copyable.  Note that Selector is a class that wraps a
PointerUnion.


>From 60d190e733b7a0f333bcb6676d0c701db1370d90 Mon Sep 17 00:00:00 2001
From: Kazu Hirata <kazu at google.com>
Date: Thu, 19 Sep 2024 09:28:49 -0700
Subject: [PATCH] [Sema] Avoid repeated hash lookups (NFC)

GlobalMethodPool, the type of MethodPool, is a type wrapping DenseMap
and exposes only a subset of the DenseMap methods.

This patch adds operator[] to GlobalMethodPool so that we can avoid
repeated hash lookups.  I don't bother using references or rvalue
references in operator[] because Selector, the key type, is small and
trivially copyable.  Note that Selector is a class that wraps a
PointerUnion.
---
 clang/include/clang/Sema/SemaObjC.h | 1 +
 clang/lib/Sema/SemaDeclObjC.cpp     | 9 ++-------
 2 files changed, 3 insertions(+), 7 deletions(-)

diff --git a/clang/include/clang/Sema/SemaObjC.h b/clang/include/clang/Sema/SemaObjC.h
index 213c37b5091fe0..b868e9e7cd0aa9 100644
--- a/clang/include/clang/Sema/SemaObjC.h
+++ b/clang/include/clang/Sema/SemaObjC.h
@@ -218,6 +218,7 @@ class SemaObjC : public SemaBase {
     std::pair<iterator, bool> insert(std::pair<Selector, Lists> &&Val) {
       return Methods.insert(Val);
     }
+    Lists &operator[](Selector Key) { return Methods[Key]; }
     int count(Selector Sel) const { return Methods.count(Sel); }
     bool empty() const { return Methods.empty(); }
 
diff --git a/clang/lib/Sema/SemaDeclObjC.cpp b/clang/lib/Sema/SemaDeclObjC.cpp
index 807453400abdd0..87be43b13813d3 100644
--- a/clang/lib/Sema/SemaDeclObjC.cpp
+++ b/clang/lib/Sema/SemaDeclObjC.cpp
@@ -3441,16 +3441,11 @@ void SemaObjC::AddMethodToGlobalPool(ObjCMethodDecl *Method, bool impl,
   if (SemaRef.ExternalSource)
     ReadMethodPool(Method->getSelector());
 
-  GlobalMethodPool::iterator Pos = MethodPool.find(Method->getSelector());
-  if (Pos == MethodPool.end())
-    Pos = MethodPool
-              .insert(std::make_pair(Method->getSelector(),
-                                     GlobalMethodPool::Lists()))
-              .first;
+  auto &Lists = MethodPool[Method->getSelector()];
 
   Method->setDefined(impl);
 
-  ObjCMethodList &Entry = instance ? Pos->second.first : Pos->second.second;
+  ObjCMethodList &Entry = instance ? Lists.first : Lists.second;
   addMethodToGlobalList(&Entry, Method);
 }
 



More information about the cfe-commits mailing list