Skip to content

Commit 8fd4f8e

Browse files
[ADT] Consolidate the grow() logic in DenseMapBase (NFC)
This patch consolidates the grow() logic in DenseMapBase::grow. With this patch, DenseMapBase::grow() creates a temporary grown instance and then lets DenseMap/SmallDenseMap attempt to move the instance back to *this. If it doesn't work, we move again. The "attempt to move" always succeeds for DenseMap. For SmallDenseMap, it succeeds only in the large mode. This is part of the effort outlined in #168255.
1 parent 49d5bb0 commit 8fd4f8e

File tree

1 file changed

+18
-21
lines changed

1 file changed

+18
-21
lines changed

llvm/include/llvm/ADT/DenseMap.h

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -558,7 +558,12 @@ class DenseMapBase : public DebugEpochBase {
558558

559559
void grow(unsigned MinNumBuckets) {
560560
unsigned NumBuckets = DerivedT::roundUpNumBuckets(MinNumBuckets);
561-
derived().grow(NumBuckets);
561+
DerivedT Tmp(NumBuckets, ExactBucketCount{});
562+
Tmp.moveFrom(derived());
563+
if (derived().maybeMoveFast(std::move(Tmp)))
564+
return;
565+
initWithExactBucketCount(NumBuckets);
566+
moveFrom(Tmp);
562567
}
563568

564569
template <typename LookupKeyT>
@@ -848,10 +853,9 @@ class DenseMap : public DenseMapBase<DenseMap<KeyT, ValueT, KeyInfoT, BucketT>,
848853
static_cast<unsigned>(NextPowerOf2(MinNumBuckets - 1)));
849854
}
850855

851-
void grow(unsigned AtLeast) {
852-
DenseMap Tmp(AtLeast, typename BaseT::ExactBucketCount{});
853-
Tmp.moveFrom(*this);
854-
swapImpl(Tmp);
856+
bool maybeMoveFast(DenseMap &&Other) {
857+
swapImpl(Other);
858+
return true;
855859
}
856860

857861
// Plan how to shrink the bucket table. Return:
@@ -1120,23 +1124,16 @@ class SmallDenseMap
11201124
static_cast<unsigned>(NextPowerOf2(MinNumBuckets - 1)));
11211125
}
11221126

1123-
void grow(unsigned NumBuckets) {
1124-
SmallDenseMap Tmp(NumBuckets, typename BaseT::ExactBucketCount{});
1125-
Tmp.moveFrom(*this);
1127+
bool maybeMoveFast(SmallDenseMap &&Other) {
1128+
if (Other.Small)
1129+
return false;
11261130

1127-
if (Tmp.Small) {
1128-
// Use moveFrom in those rare cases where we stay in the small mode. This
1129-
// can happen when we have many tombstones.
1130-
Small = true;
1131-
this->BaseT::initEmpty();
1132-
this->moveFrom(Tmp);
1133-
} else {
1134-
Small = false;
1135-
NumEntries = Tmp.NumEntries;
1136-
NumTombstones = Tmp.NumTombstones;
1137-
*getLargeRep() = std::move(*Tmp.getLargeRep());
1138-
Tmp.getLargeRep()->NumBuckets = 0;
1139-
}
1131+
Small = false;
1132+
NumEntries = Other.NumEntries;
1133+
NumTombstones = Other.NumTombstones;
1134+
*getLargeRep() = std::move(*Other.getLargeRep());
1135+
Other.getLargeRep()->NumBuckets = 0;
1136+
return true;
11401137
}
11411138

11421139
// Plan how to shrink the bucket table. Return:

0 commit comments

Comments
 (0)