Improve SmallPtrSetImpl::count implementation
authorserge-sans-paille <sguelton@redhat.com>
Mon, 1 Jun 2020 05:49:19 +0000 (07:49 +0200)
committerserge-sans-paille <sguelton@redhat.com>
Mon, 1 Jun 2020 05:49:19 +0000 (07:49 +0200)
Relying on the find method implies a roundtrip to the iterator world, which is
not costless because iterator creation involves a few check to ensure the
iterator is in a valid position (through the SmallPtrSetIteratorImpl::AdvanceIfNotValid
method). It turns out that the result of SmallPtrSetImpl::find_imp is either
valid or the EndPointer, so there's no need to go through that abstraction,
and the compiler cannot guess it.

Differential Revision: https://reviews.llvm.org/D80708

llvm/include/llvm/ADT/SmallPtrSet.h

index 05073cf..0ab05cf 100644 (file)
@@ -372,7 +372,9 @@ public:
     return erase_imp(PtrTraits::getAsVoidPointer(Ptr));
   }
   /// count - Return 1 if the specified pointer is in the set, 0 otherwise.
-  size_type count(ConstPtrType Ptr) const { return find(Ptr) != end() ? 1 : 0; }
+  size_type count(ConstPtrType Ptr) const {
+    return find_imp(ConstPtrTraits::getAsVoidPointer(Ptr)) != EndPointer();
+  }
   iterator find(ConstPtrType Ptr) const {
     return makeIterator(find_imp(ConstPtrTraits::getAsVoidPointer(Ptr)));
   }