Skip to content
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

[SmallPtrSet] Optimize find/erase #104740

Merged
merged 1 commit into from
Aug 19, 2024
Merged
Show file tree
Hide file tree
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
8 changes: 4 additions & 4 deletions llvm/include/llvm/ADT/SmallPtrSet.h
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,8 @@ class SmallPtrSetImplBase : public DebugEpochBase {
return false;
}

auto *Bucket = FindBucketFor(Ptr);
if (*Bucket != Ptr)
auto *Bucket = doFind(Ptr);
if (!Bucket)
return false;

*const_cast<const void **>(Bucket) = getTombstoneMarker();
Expand All @@ -211,8 +211,7 @@ class SmallPtrSetImplBase : public DebugEpochBase {
}

// Big set case.
auto *Bucket = FindBucketFor(Ptr);
if (*Bucket == Ptr)
if (auto *Bucket = doFind(Ptr))
return Bucket;
return EndPointer();
}
Expand All @@ -222,6 +221,7 @@ class SmallPtrSetImplBase : public DebugEpochBase {
private:
std::pair<const void *const *, bool> insert_imp_big(const void *Ptr);

const void *const *doFind(const void *Ptr) const;
const void * const *FindBucketFor(const void *Ptr) const;
void shrink_and_clear();

Expand Down
20 changes: 19 additions & 1 deletion llvm/lib/Support/SmallPtrSet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,25 @@ SmallPtrSetImplBase::insert_imp_big(const void *Ptr) {
return std::make_pair(Bucket, true);
}

const void * const *SmallPtrSetImplBase::FindBucketFor(const void *Ptr) const {
const void *const *SmallPtrSetImplBase::doFind(const void *Ptr) const {
unsigned BucketNo =
DenseMapInfo<void *>::getHashValue(Ptr) & (CurArraySize - 1);
unsigned ProbeAmt = 1;
while (true) {
const void *const *Bucket = CurArray + BucketNo;
if (LLVM_LIKELY(*Bucket == Ptr))
return Bucket;
if (LLVM_LIKELY(*Bucket == getEmptyMarker()))
return nullptr;

// Otherwise, it's a hash collision or a tombstone, continue quadratic
// probing.
BucketNo += ProbeAmt++;
BucketNo &= CurArraySize - 1;
}
}

const void *const *SmallPtrSetImplBase::FindBucketFor(const void *Ptr) const {
unsigned Bucket = DenseMapInfo<void *>::getHashValue(Ptr) & (CurArraySize-1);
unsigned ArraySize = CurArraySize;
unsigned ProbeAmt = 1;
Expand Down
Loading