Skip to content

Commit 3c54972

Browse files
[ADT] Remove DenseMap::init (NFC) (#168322)
This patch removes DenseMap::init and SmallDenseMap::init by inlining them into their call sites and simplifying them. init() is defined as: void init(unsigned InitNumEntries) { auto InitBuckets = BaseT::getMinBucketToReserveForEntries(InitNumEntries); this->initWithExactBucketCount(InitBuckets); } - Constuctors: Now that we have constructors that allocate the exact number of buckets (as opposed to the number of key/value pairs), init() does too much. Once we convert the number of key/value pairs to the number of buckets, we can call the constructors that take the exact number of buckets. - init(0) in the move assignment operators simplifies down to: initWithExactBucketCount(0) - shrink_and_clear() computes the number of buckets to have after the clear operation. As such, we should call initWithExactBucketCount, not init. Otherwise, we would end up adding "load factor padding" on top of NewNumBuckets: NextPowerOf2(NewNumBuckets * 4 / 3 + 1) All in all, init() doesn't bring any value in the current setup. This patch is part of the effort outlined in #168255.
1 parent dcf8cd9 commit 3c54972

File tree

1 file changed

+10
-19
lines changed

1 file changed

+10
-19
lines changed

llvm/include/llvm/ADT/DenseMap.h

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ class DenseMapBase : public DebugEpochBase {
162162
return;
163163
}
164164
derived().deallocateBuckets();
165-
derived().init(NewNumBuckets);
165+
initWithExactBucketCount(NewNumBuckets);
166166
}
167167

168168
/// Return true if the specified key is in the map, false otherwise.
@@ -755,9 +755,9 @@ class DenseMap : public DenseMapBase<DenseMap<KeyT, ValueT, KeyInfoT, BucketT>,
755755
public:
756756
/// Create a DenseMap with an optional \p NumElementsToReserve to guarantee
757757
/// that this number of elements can be inserted in the map without grow().
758-
explicit DenseMap(unsigned NumElementsToReserve = 0) {
759-
init(NumElementsToReserve);
760-
}
758+
explicit DenseMap(unsigned NumElementsToReserve = 0)
759+
: DenseMap(BaseT::getMinBucketToReserveForEntries(NumElementsToReserve),
760+
typename BaseT::ExactBucketCount{}) {}
761761

762762
DenseMap(const DenseMap &other) : DenseMap() { this->copyFrom(other); }
763763

@@ -789,7 +789,7 @@ class DenseMap : public DenseMapBase<DenseMap<KeyT, ValueT, KeyInfoT, BucketT>,
789789
DenseMap &operator=(DenseMap &&other) {
790790
this->destroyAll();
791791
deallocateBuckets();
792-
init(0);
792+
this->initWithExactBucketCount(0);
793793
this->swap(other);
794794
return *this;
795795
}
@@ -830,11 +830,6 @@ class DenseMap : public DenseMapBase<DenseMap<KeyT, ValueT, KeyInfoT, BucketT>,
830830
return true;
831831
}
832832

833-
void init(unsigned InitNumEntries) {
834-
auto InitBuckets = BaseT::getMinBucketToReserveForEntries(InitNumEntries);
835-
this->initWithExactBucketCount(InitBuckets);
836-
}
837-
838833
// Put the zombie instance in a known good state after a move.
839834
void kill() {
840835
deallocateBuckets();
@@ -902,9 +897,10 @@ class SmallDenseMap
902897
}
903898

904899
public:
905-
explicit SmallDenseMap(unsigned NumElementsToReserve = 0) {
906-
init(NumElementsToReserve);
907-
}
900+
explicit SmallDenseMap(unsigned NumElementsToReserve = 0)
901+
: SmallDenseMap(
902+
BaseT::getMinBucketToReserveForEntries(NumElementsToReserve),
903+
typename BaseT::ExactBucketCount{}) {}
908904

909905
SmallDenseMap(const SmallDenseMap &other) : SmallDenseMap() {
910906
this->copyFrom(other);
@@ -939,7 +935,7 @@ class SmallDenseMap
939935
SmallDenseMap &operator=(SmallDenseMap &&other) {
940936
this->destroyAll();
941937
deallocateBuckets();
942-
init(0);
938+
this->initWithExactBucketCount(0);
943939
this->swap(other);
944940
return *this;
945941
}
@@ -1095,11 +1091,6 @@ class SmallDenseMap
10951091
return true;
10961092
}
10971093

1098-
void init(unsigned InitNumEntries) {
1099-
auto InitBuckets = BaseT::getMinBucketToReserveForEntries(InitNumEntries);
1100-
this->initWithExactBucketCount(InitBuckets);
1101-
}
1102-
11031094
// Put the zombie instance in a known good state after a move.
11041095
void kill() {
11051096
deallocateBuckets();

0 commit comments

Comments
 (0)