Skip to content

Commit dcf8cd9

Browse files
[ADT] Consolidate the grow() logic in DenseMapBase (NFC) (#168316)
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 bf21156 commit dcf8cd9

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>
@@ -842,10 +847,9 @@ class DenseMap : public DenseMapBase<DenseMap<KeyT, ValueT, KeyInfoT, BucketT>,
842847
static_cast<unsigned>(NextPowerOf2(MinNumBuckets - 1)));
843848
}
844849

845-
void grow(unsigned AtLeast) {
846-
DenseMap Tmp(AtLeast, typename BaseT::ExactBucketCount{});
847-
Tmp.moveFrom(*this);
848-
swapImpl(Tmp);
850+
bool maybeMoveFast(DenseMap &&Other) {
851+
swapImpl(Other);
852+
return true;
849853
}
850854

851855
// Plan how to shrink the bucket table. Return:
@@ -1110,23 +1114,16 @@ class SmallDenseMap
11101114
static_cast<unsigned>(NextPowerOf2(MinNumBuckets - 1)));
11111115
}
11121116

1113-
void grow(unsigned NumBuckets) {
1114-
SmallDenseMap Tmp(NumBuckets, typename BaseT::ExactBucketCount{});
1115-
Tmp.moveFrom(*this);
1117+
bool maybeMoveFast(SmallDenseMap &&Other) {
1118+
if (Other.Small)
1119+
return false;
11161120

1117-
if (Tmp.Small) {
1118-
// Use moveFrom in those rare cases where we stay in the small mode. This
1119-
// can happen when we have many tombstones.
1120-
Small = true;
1121-
this->BaseT::initEmpty();
1122-
this->moveFrom(Tmp);
1123-
} else {
1124-
Small = false;
1125-
NumEntries = Tmp.NumEntries;
1126-
NumTombstones = Tmp.NumTombstones;
1127-
*getLargeRep() = std::move(*Tmp.getLargeRep());
1128-
Tmp.getLargeRep()->NumBuckets = 0;
1129-
}
1121+
Small = false;
1122+
NumEntries = Other.NumEntries;
1123+
NumTombstones = Other.NumTombstones;
1124+
*getLargeRep() = std::move(*Other.getLargeRep());
1125+
Other.getLargeRep()->NumBuckets = 0;
1126+
return true;
11301127
}
11311128

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

0 commit comments

Comments
 (0)