Skip to content

Commit 6bfb6d4

Browse files
authored
[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%.
1 parent 0ad6be1 commit 6bfb6d4

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

llvm/include/llvm/ADT/SmallPtrSet.h

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,20 @@ class SmallPtrSetImplBase : public DebugEpochBase {
216216
return EndPointer();
217217
}
218218

219+
bool contains_imp(const void *Ptr) const {
220+
if (isSmall()) {
221+
// Linear search for the item.
222+
const void *const *APtr = SmallArray;
223+
const void *const *E = SmallArray + NumNonEmpty;
224+
for (; APtr != E; ++APtr)
225+
if (*APtr == Ptr)
226+
return true;
227+
return false;
228+
}
229+
230+
return doFind(Ptr) != nullptr;
231+
}
232+
219233
bool isSmall() const { return CurArray == SmallArray; }
220234

221235
private:
@@ -433,13 +447,13 @@ class SmallPtrSetImpl : public SmallPtrSetImplBase {
433447

434448
/// count - Return 1 if the specified pointer is in the set, 0 otherwise.
435449
size_type count(ConstPtrType Ptr) const {
436-
return find_imp(ConstPtrTraits::getAsVoidPointer(Ptr)) != EndPointer();
450+
return contains_imp(ConstPtrTraits::getAsVoidPointer(Ptr));
437451
}
438452
iterator find(ConstPtrType Ptr) const {
439453
return makeIterator(find_imp(ConstPtrTraits::getAsVoidPointer(Ptr)));
440454
}
441455
bool contains(ConstPtrType Ptr) const {
442-
return find_imp(ConstPtrTraits::getAsVoidPointer(Ptr)) != EndPointer();
456+
return contains_imp(ConstPtrTraits::getAsVoidPointer(Ptr));
443457
}
444458

445459
template <typename IterT>

0 commit comments

Comments
 (0)