Skip to content

Commit a3fea06

Browse files
authored
[SmallPtrSet] Optimize find/erase
Port #100517 for DenseMap. Pull Request: #104740
1 parent f95026d commit a3fea06

File tree

2 files changed

+23
-5
lines changed

2 files changed

+23
-5
lines changed

llvm/include/llvm/ADT/SmallPtrSet.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -185,8 +185,8 @@ class SmallPtrSetImplBase : public DebugEpochBase {
185185
return false;
186186
}
187187

188-
auto *Bucket = FindBucketFor(Ptr);
189-
if (*Bucket != Ptr)
188+
auto *Bucket = doFind(Ptr);
189+
if (!Bucket)
190190
return false;
191191

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

213213
// Big set case.
214-
auto *Bucket = FindBucketFor(Ptr);
215-
if (*Bucket == Ptr)
214+
if (auto *Bucket = doFind(Ptr))
216215
return Bucket;
217216
return EndPointer();
218217
}
@@ -222,6 +221,7 @@ class SmallPtrSetImplBase : public DebugEpochBase {
222221
private:
223222
std::pair<const void *const *, bool> insert_imp_big(const void *Ptr);
224223

224+
const void *const *doFind(const void *Ptr) const;
225225
const void * const *FindBucketFor(const void *Ptr) const;
226226
void shrink_and_clear();
227227

llvm/lib/Support/SmallPtrSet.cpp

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,25 @@ SmallPtrSetImplBase::insert_imp_big(const void *Ptr) {
6262
return std::make_pair(Bucket, true);
6363
}
6464

65-
const void * const *SmallPtrSetImplBase::FindBucketFor(const void *Ptr) const {
65+
const void *const *SmallPtrSetImplBase::doFind(const void *Ptr) const {
66+
unsigned BucketNo =
67+
DenseMapInfo<void *>::getHashValue(Ptr) & (CurArraySize - 1);
68+
unsigned ProbeAmt = 1;
69+
while (true) {
70+
const void *const *Bucket = CurArray + BucketNo;
71+
if (LLVM_LIKELY(*Bucket == Ptr))
72+
return Bucket;
73+
if (LLVM_LIKELY(*Bucket == getEmptyMarker()))
74+
return nullptr;
75+
76+
// Otherwise, it's a hash collision or a tombstone, continue quadratic
77+
// probing.
78+
BucketNo += ProbeAmt++;
79+
BucketNo &= CurArraySize - 1;
80+
}
81+
}
82+
83+
const void *const *SmallPtrSetImplBase::FindBucketFor(const void *Ptr) const {
6684
unsigned Bucket = DenseMapInfo<void *>::getHashValue(Ptr) & (CurArraySize-1);
6785
unsigned ArraySize = CurArraySize;
6886
unsigned ProbeAmt = 1;

0 commit comments

Comments
 (0)