Skip to content

Conversation

@nikic
Copy link
Contributor

@nikic nikic commented Nov 29, 2024

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 nice compile-time improvement: https://llvm-compile-time-tracker.com/compare.php?from=352f8688d0ca250c9e8774321f6c3bcd4298cc09&to=80f10c61f8a4042e03b5873c35c46eb905a76d4a&stat=instructions:u

@llvmbot
Copy link
Member

llvmbot commented Nov 29, 2024

@llvm/pr-subscribers-llvm-adt

Author: Nikita Popov (nikic)

Changes

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 nice compile-time improvement: https://llvm-compile-time-tracker.com/compare.php?from=352f8688d0ca250c9e8774321f6c3bcd4298cc09&to=80f10c61f8a4042e03b5873c35c46eb905a76d4a&stat=instructions:u


Full diff: https://github.com/llvm/llvm-project/pull/118092.diff

1 Files Affected:

  • (modified) llvm/include/llvm/ADT/SmallPtrSet.h (+15-2)
diff --git a/llvm/include/llvm/ADT/SmallPtrSet.h b/llvm/include/llvm/ADT/SmallPtrSet.h
index 1fc2318342ae78..5f4e5d3445ecec 100644
--- a/llvm/include/llvm/ADT/SmallPtrSet.h
+++ b/llvm/include/llvm/ADT/SmallPtrSet.h
@@ -216,6 +216,19 @@ 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 +446,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>

@github-actions
Copy link

github-actions bot commented Nov 29, 2024

✅ With the latest revision this PR passed the C/C++ code formatter.

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().
@nikic nikic force-pushed the smallptrset-contains branch from c45480c to 6e97583 Compare November 29, 2024 13:30
Copy link
Member

@kuhar kuhar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, nice!

@MaskRay
Copy link
Member

MaskRay commented Nov 29, 2024

Nice. It seems that doFind is in .cpp and cannot be optimized.

@nikic nikic merged commit 6bfb6d4 into llvm:main Nov 29, 2024
8 checks passed
@nikic nikic deleted the smallptrset-contains branch November 29, 2024 20:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants