[llvm] 11efb08 - Improve SmallPtrSetImpl::count implementation

via llvm-commits llvm-commits at lists.llvm.org
Sun May 31 22:51:52 PDT 2020


Author: serge-sans-paille
Date: 2020-06-01T07:49:19+02:00
New Revision: 11efb0837c897c709ae162eb5ebabb460fc537ff

URL: https://github.com/llvm/llvm-project/commit/11efb0837c897c709ae162eb5ebabb460fc537ff
DIFF: https://github.com/llvm/llvm-project/commit/11efb0837c897c709ae162eb5ebabb460fc537ff.diff

LOG: Improve SmallPtrSetImpl::count implementation

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

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 05073cf17f92..0ab05cfe611a 100644
--- a/llvm/include/llvm/ADT/SmallPtrSet.h
+++ b/llvm/include/llvm/ADT/SmallPtrSet.h
@@ -372,7 +372,9 @@ class SmallPtrSetImpl : public SmallPtrSetImplBase {
     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)));
   }


        


More information about the llvm-commits mailing list