Skip to content

[SmallPtrSet] Optimize contains (NFC) #118092

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 29, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions llvm/include/llvm/ADT/SmallPtrSet.h
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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>
Expand Down