[llvm] 6bfb6d4 - [SmallPtrSet] Optimize contains (NFC) (#118092)

via llvm-commits llvm-commits at lists.llvm.org
Fri Nov 29 12:54:03 PST 2024


Author: Nikita Popov
Date: 2024-11-29T21:54:00+01:00
New Revision: 6bfb6d4092a284e1fcd135625c0e713d019f0572

URL: https://github.com/llvm/llvm-project/commit/6bfb6d4092a284e1fcd135625c0e713d019f0572
DIFF: https://github.com/llvm/llvm-project/commit/6bfb6d4092a284e1fcd135625c0e713d019f0572.diff

LOG: [SmallPtrSet] Optimize contains (NFC) (#118092)

Instead of going through find_imp(), implement a specialized
contains_imp() that directly returns a boolean instead of a pointer that
needs to be compared to EndPointer().

This gives a compile-time improvement of around 0.2-0.3%.

Added: 
    

Modified: 
    llvm/include/llvm/ADT/SmallPtrSet.h

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/ADT/SmallPtrSet.h b/llvm/include/llvm/ADT/SmallPtrSet.h
index 1fc2318342ae78..b15845f3b76a7e 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.
+      const void *const *APtr = SmallArray;
+      const void *const *E = SmallArray + NumNonEmpty;
+      for (; 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>


        


More information about the llvm-commits mailing list