Skip to content

Commit 4206558

Browse files
[ADT] Add roundUpNumBuckets to DenseMap (NFC) (#168301)
This patch adds computeNumBuckets, a helper function to compute the number of buckets. This is part of the effort outlined in #168255. This makes it easier to move the core logic of grow() to DenseMapBase::grow().
1 parent 6152a8b commit 4206558

File tree

1 file changed

+17
-6
lines changed

1 file changed

+17
-6
lines changed

llvm/include/llvm/ADT/DenseMap.h

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -556,7 +556,10 @@ class DenseMapBase : public DebugEpochBase {
556556
return llvm::make_range(getBuckets(), getBucketsEnd());
557557
}
558558

559-
void grow(unsigned AtLeast) { derived().grow(AtLeast); }
559+
void grow(unsigned MinNumBuckets) {
560+
unsigned NumBuckets = DerivedT::roundUpNumBuckets(MinNumBuckets);
561+
derived().grow(NumBuckets);
562+
}
560563

561564
template <typename LookupKeyT>
562565
BucketT *findBucketForInsertion(const LookupKeyT &Lookup,
@@ -840,8 +843,12 @@ class DenseMap : public DenseMapBase<DenseMap<KeyT, ValueT, KeyInfoT, BucketT>,
840843
NumBuckets = 0;
841844
}
842845

846+
static unsigned roundUpNumBuckets(unsigned MinNumBuckets) {
847+
return std::max(64u,
848+
static_cast<unsigned>(NextPowerOf2(MinNumBuckets - 1)));
849+
}
850+
843851
void grow(unsigned AtLeast) {
844-
AtLeast = std::max<unsigned>(64, NextPowerOf2(AtLeast - 1));
845852
DenseMap Tmp(AtLeast, typename BaseT::ExactBucketCount{});
846853
Tmp.moveFrom(*this);
847854
swapImpl(Tmp);
@@ -1106,11 +1113,15 @@ class SmallDenseMap
11061113
new (getLargeRep()) LargeRep{nullptr, 0};
11071114
}
11081115

1109-
void grow(unsigned AtLeast) {
1110-
if (AtLeast > InlineBuckets)
1111-
AtLeast = std::max<unsigned>(64, NextPowerOf2(AtLeast - 1));
1116+
static unsigned roundUpNumBuckets(unsigned MinNumBuckets) {
1117+
if (MinNumBuckets <= InlineBuckets)
1118+
return MinNumBuckets;
1119+
return std::max(64u,
1120+
static_cast<unsigned>(NextPowerOf2(MinNumBuckets - 1)));
1121+
}
11121122

1113-
SmallDenseMap Tmp(AtLeast, typename BaseT::ExactBucketCount{});
1123+
void grow(unsigned NumBuckets) {
1124+
SmallDenseMap Tmp(NumBuckets, typename BaseT::ExactBucketCount{});
11141125
Tmp.moveFrom(*this);
11151126

11161127
if (Tmp.Small) {

0 commit comments

Comments
 (0)