Skip to content

Commit e490920

Browse files
[ADT] Remove KeyInfoT forwarders in DenseMap.h (NFC) (#165102)
This patch removes getEmptyKey, getTombstoneKey, and getHashValue from DenseMapBase. These forwarder methods do not really encapsulate KeyInfoT. Many of their callers already mention KeyInfoT::isEqual for example. An existing static_assert is moved to another method. Note that it must live in a method for type completeness reasons.
1 parent c3a4093 commit e490920

File tree

1 file changed

+23
-33
lines changed

1 file changed

+23
-33
lines changed

llvm/include/llvm/ADT/DenseMap.h

Lines changed: 23 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -130,13 +130,13 @@ class DenseMapBase : public DebugEpochBase {
130130
return;
131131
}
132132

133-
const KeyT EmptyKey = getEmptyKey();
133+
const KeyT EmptyKey = KeyInfoT::getEmptyKey();
134134
if constexpr (std::is_trivially_destructible_v<ValueT>) {
135135
// Use a simpler loop when values don't need destruction.
136136
for (BucketT &B : buckets())
137137
B.getFirst() = EmptyKey;
138138
} else {
139-
const KeyT TombstoneKey = getTombstoneKey();
139+
const KeyT TombstoneKey = KeyInfoT::getTombstoneKey();
140140
unsigned NumEntries = getNumEntries();
141141
for (BucketT &B : buckets()) {
142142
if (!KeyInfoT::isEqual(B.getFirst(), EmptyKey)) {
@@ -314,15 +314,15 @@ class DenseMapBase : public DebugEpochBase {
314314
return false; // not in map.
315315

316316
TheBucket->getSecond().~ValueT();
317-
TheBucket->getFirst() = getTombstoneKey();
317+
TheBucket->getFirst() = KeyInfoT::getTombstoneKey();
318318
decrementNumEntries();
319319
incrementNumTombstones();
320320
return true;
321321
}
322322
void erase(iterator I) {
323323
BucketT *TheBucket = &*I;
324324
TheBucket->getSecond().~ValueT();
325-
TheBucket->getFirst() = getTombstoneKey();
325+
TheBucket->getFirst() = KeyInfoT::getTombstoneKey();
326326
decrementNumEntries();
327327
incrementNumTombstones();
328328
}
@@ -362,7 +362,8 @@ class DenseMapBase : public DebugEpochBase {
362362
if (getNumBuckets() == 0) // Nothing to do.
363363
return;
364364

365-
const KeyT EmptyKey = getEmptyKey(), TombstoneKey = getTombstoneKey();
365+
const KeyT EmptyKey = KeyInfoT::getEmptyKey();
366+
const KeyT TombstoneKey = KeyInfoT::getTombstoneKey();
366367
for (BucketT &B : buckets()) {
367368
if (!KeyInfoT::isEqual(B.getFirst(), EmptyKey) &&
368369
!KeyInfoT::isEqual(B.getFirst(), TombstoneKey))
@@ -372,12 +373,14 @@ class DenseMapBase : public DebugEpochBase {
372373
}
373374

374375
void initEmpty() {
376+
static_assert(std::is_base_of_v<DenseMapBase, DerivedT>,
377+
"Must pass the derived type to this template!");
375378
setNumEntries(0);
376379
setNumTombstones(0);
377380

378381
assert((getNumBuckets() & (getNumBuckets() - 1)) == 0 &&
379382
"# initial buckets must be a power of two!");
380-
const KeyT EmptyKey = getEmptyKey();
383+
const KeyT EmptyKey = KeyInfoT::getEmptyKey();
381384
for (BucketT &B : buckets())
382385
::new (&B.getFirst()) KeyT(EmptyKey);
383386
}
@@ -397,8 +400,8 @@ class DenseMapBase : public DebugEpochBase {
397400
initEmpty();
398401

399402
// Insert all the old elements.
400-
const KeyT EmptyKey = getEmptyKey();
401-
const KeyT TombstoneKey = getTombstoneKey();
403+
const KeyT EmptyKey = KeyInfoT::getEmptyKey();
404+
const KeyT TombstoneKey = KeyInfoT::getTombstoneKey();
402405
for (BucketT &B : OldBuckets) {
403406
if (!KeyInfoT::isEqual(B.getFirst(), EmptyKey) &&
404407
!KeyInfoT::isEqual(B.getFirst(), TombstoneKey)) {
@@ -442,8 +445,8 @@ class DenseMapBase : public DebugEpochBase {
442445
memcpy(reinterpret_cast<void *>(Buckets), OtherBuckets,
443446
NumBuckets * sizeof(BucketT));
444447
} else {
445-
const KeyT EmptyKey = getEmptyKey();
446-
const KeyT TombstoneKey = getTombstoneKey();
448+
const KeyT EmptyKey = KeyInfoT::getEmptyKey();
449+
const KeyT TombstoneKey = KeyInfoT::getTombstoneKey();
447450
for (size_t I = 0; I < NumBuckets; ++I) {
448451
::new (&Buckets[I].getFirst()) KeyT(OtherBuckets[I].getFirst());
449452
if (!KeyInfoT::isEqual(Buckets[I].getFirst(), EmptyKey) &&
@@ -453,19 +456,6 @@ class DenseMapBase : public DebugEpochBase {
453456
}
454457
}
455458

456-
template <typename LookupKeyT>
457-
static unsigned getHashValue(const LookupKeyT &Val) {
458-
return KeyInfoT::getHashValue(Val);
459-
}
460-
461-
static const KeyT getEmptyKey() {
462-
static_assert(std::is_base_of_v<DenseMapBase, DerivedT>,
463-
"Must pass the derived type to this template!");
464-
return KeyInfoT::getEmptyKey();
465-
}
466-
467-
static const KeyT getTombstoneKey() { return KeyInfoT::getTombstoneKey(); }
468-
469459
private:
470460
DerivedT &derived() { return *static_cast<DerivedT *>(this); }
471461
const DerivedT &derived() const {
@@ -573,7 +563,7 @@ class DenseMapBase : public DebugEpochBase {
573563
incrementNumEntries();
574564

575565
// If we are writing over a tombstone, remember this.
576-
const KeyT EmptyKey = getEmptyKey();
566+
const KeyT EmptyKey = KeyInfoT::getEmptyKey();
577567
if (!KeyInfoT::isEqual(TheBucket->getFirst(), EmptyKey))
578568
decrementNumTombstones();
579569

@@ -587,8 +577,8 @@ class DenseMapBase : public DebugEpochBase {
587577
if (NumBuckets == 0)
588578
return nullptr;
589579

590-
const KeyT EmptyKey = getEmptyKey();
591-
unsigned BucketNo = getHashValue(Val) & (NumBuckets - 1);
580+
const KeyT EmptyKey = KeyInfoT::getEmptyKey();
581+
unsigned BucketNo = KeyInfoT::getHashValue(Val) & (NumBuckets - 1);
592582
unsigned ProbeAmt = 1;
593583
while (true) {
594584
const BucketT *Bucket = BucketsPtr + BucketNo;
@@ -625,13 +615,13 @@ class DenseMapBase : public DebugEpochBase {
625615

626616
// FoundTombstone - Keep track of whether we find a tombstone while probing.
627617
BucketT *FoundTombstone = nullptr;
628-
const KeyT EmptyKey = getEmptyKey();
629-
const KeyT TombstoneKey = getTombstoneKey();
618+
const KeyT EmptyKey = KeyInfoT::getEmptyKey();
619+
const KeyT TombstoneKey = KeyInfoT::getTombstoneKey();
630620
assert(!KeyInfoT::isEqual(Val, EmptyKey) &&
631621
!KeyInfoT::isEqual(Val, TombstoneKey) &&
632622
"Empty/Tombstone value shouldn't be inserted into map!");
633623

634-
unsigned BucketNo = getHashValue(Val) & (NumBuckets - 1);
624+
unsigned BucketNo = KeyInfoT::getHashValue(Val) & (NumBuckets - 1);
635625
unsigned ProbeAmt = 1;
636626
while (true) {
637627
BucketT *ThisBucket = BucketsPtr + BucketNo;
@@ -930,8 +920,8 @@ class SmallDenseMap
930920
NumEntries = TmpNumEntries;
931921
std::swap(NumTombstones, RHS.NumTombstones);
932922

933-
const KeyT EmptyKey = this->getEmptyKey();
934-
const KeyT TombstoneKey = this->getTombstoneKey();
923+
const KeyT EmptyKey = KeyInfoT::getEmptyKey();
924+
const KeyT TombstoneKey = KeyInfoT::getTombstoneKey();
935925
if (Small && RHS.Small) {
936926
// If we're swapping inline bucket arrays, we have to cope with some of
937927
// the tricky bits of DenseMap's storage system: the buckets are not
@@ -1125,8 +1115,8 @@ class SmallDenseMap
11251115

11261116
// Loop over the buckets, moving non-empty, non-tombstones into the
11271117
// temporary storage. Have the loop move the TmpEnd forward as it goes.
1128-
const KeyT EmptyKey = this->getEmptyKey();
1129-
const KeyT TombstoneKey = this->getTombstoneKey();
1118+
const KeyT EmptyKey = KeyInfoT::getEmptyKey();
1119+
const KeyT TombstoneKey = KeyInfoT::getTombstoneKey();
11301120
for (BucketT &B : inlineBuckets()) {
11311121
if (!KeyInfoT::isEqual(B.getFirst(), EmptyKey) &&
11321122
!KeyInfoT::isEqual(B.getFirst(), TombstoneKey)) {

0 commit comments

Comments
 (0)