[llvm] [SmallPtrSet] Optimize contains (NFC) (PR #118092)

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Fri Nov 29 07:35:09 PST 2024


https://github.com/nikic updated https://github.com/llvm/llvm-project/pull/118092

>From 6e97583663958331725b54d2dcc81937cd9d51b0 Mon Sep 17 00:00:00 2001
From: Nikita Popov <npopov at redhat.com>
Date: Fri, 29 Nov 2024 12:00:11 +0100
Subject: [PATCH 1/2] [SmallPtrSet] Optimize contains()

Instead of going through find_imp(), implement a specialized
contains_imp() that directly returns a boolean instead of a
pointer that is compared to EndPointer().
---
 llvm/include/llvm/ADT/SmallPtrSet.h | 18 ++++++++++++++++--
 1 file changed, 16 insertions(+), 2 deletions(-)

diff --git a/llvm/include/llvm/ADT/SmallPtrSet.h b/llvm/include/llvm/ADT/SmallPtrSet.h
index 1fc2318342ae78..67c3662e1d08d1 100644
--- a/llvm/include/llvm/ADT/SmallPtrSet.h
+++ b/llvm/include/llvm/ADT/SmallPtrSet.h
@@ -216,6 +216,20 @@ class SmallPtrSetImplBase : public DebugEpochBase {
     return EndPointer();
   }
 
+  bool contains_imp(const void *Ptr) const {
+    if (isSmall()) {
+      // Linear search for the item.
+      for (const void *const *APtr = SmallArray, *const *E =
+                                                     SmallArray + NumNonEmpty;
+           APtr != E; ++APtr)
+        if (*APtr == Ptr)
+          return true;
+      return false;
+    }
+
+    return doFind(Ptr) != nullptr;
+  }
+
   bool isSmall() const { return CurArray == SmallArray; }
 
 private:
@@ -433,13 +447,13 @@ class SmallPtrSetImpl : public SmallPtrSetImplBase {
 
   /// count - Return 1 if the specified pointer is in the set, 0 otherwise.
   size_type count(ConstPtrType Ptr) const {
-    return find_imp(ConstPtrTraits::getAsVoidPointer(Ptr)) != EndPointer();
+    return contains_imp(ConstPtrTraits::getAsVoidPointer(Ptr));
   }
   iterator find(ConstPtrType Ptr) const {
     return makeIterator(find_imp(ConstPtrTraits::getAsVoidPointer(Ptr)));
   }
   bool contains(ConstPtrType Ptr) const {
-    return find_imp(ConstPtrTraits::getAsVoidPointer(Ptr)) != EndPointer();
+    return contains_imp(ConstPtrTraits::getAsVoidPointer(Ptr));
   }
 
   template <typename IterT>

>From fab97d410803bc0983d8eba47d8b25d590a75ebd Mon Sep 17 00:00:00 2001
From: Nikita Popov <npopov at redhat.com>
Date: Fri, 29 Nov 2024 16:34:02 +0100
Subject: [PATCH 2/2] avoid weird formatting

---
 llvm/include/llvm/ADT/SmallPtrSet.h | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/llvm/include/llvm/ADT/SmallPtrSet.h b/llvm/include/llvm/ADT/SmallPtrSet.h
index 67c3662e1d08d1..b15845f3b76a7e 100644
--- a/llvm/include/llvm/ADT/SmallPtrSet.h
+++ b/llvm/include/llvm/ADT/SmallPtrSet.h
@@ -219,9 +219,9 @@ class SmallPtrSetImplBase : public DebugEpochBase {
   bool contains_imp(const void *Ptr) const {
     if (isSmall()) {
       // Linear search for the item.
-      for (const void *const *APtr = SmallArray, *const *E =
-                                                     SmallArray + NumNonEmpty;
-           APtr != E; ++APtr)
+      const void *const *APtr = SmallArray;
+      const void *const *E = SmallArray + NumNonEmpty;
+      for (; APtr != E; ++APtr)
         if (*APtr == Ptr)
           return true;
       return false;



More information about the llvm-commits mailing list