-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
[SmallPtrSet] Optimize find/erase #104740
Conversation
Created using spr 1.3.5-bogner
@llvm/pr-subscribers-llvm-support @llvm/pr-subscribers-llvm-adt Author: Fangrui Song (MaskRay) ChangesPort #100517 for DenseMap. Full diff: https://github.com/llvm/llvm-project/pull/104740.diff 2 Files Affected:
diff --git a/llvm/include/llvm/ADT/SmallPtrSet.h b/llvm/include/llvm/ADT/SmallPtrSet.h
index e668df08c3e636..1fc2318342ae78 100644
--- a/llvm/include/llvm/ADT/SmallPtrSet.h
+++ b/llvm/include/llvm/ADT/SmallPtrSet.h
@@ -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();
@@ -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();
}
@@ -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();
diff --git a/llvm/lib/Support/SmallPtrSet.cpp b/llvm/lib/Support/SmallPtrSet.cpp
index cbb87ea8717cfc..1bec911f39b13e 100644
--- a/llvm/lib/Support/SmallPtrSet.cpp
+++ b/llvm/lib/Support/SmallPtrSet.cpp
@@ -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;
|
stage1-O3:
stage2-O3:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM. Thanks!
Port #100517 for DenseMap.