Skip to content

Commit fdbef6a

Browse files
[ADT] Remove DenseMap::init (NFC)
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 0602678 commit fdbef6a

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.
@@ -750,9 +750,9 @@ class DenseMap : public DenseMapBase<DenseMap<KeyT, ValueT, KeyInfoT, BucketT>,
750750
public:
751751
/// Create a DenseMap with an optional \p NumElementsToReserve to guarantee
752752
/// that this number of elements can be inserted in the map without grow().
753-
explicit DenseMap(unsigned NumElementsToReserve = 0) {
754-
init(NumElementsToReserve);
755-
}
753+
explicit DenseMap(unsigned NumElementsToReserve = 0)
754+
: DenseMap(BaseT::getMinBucketToReserveForEntries(NumElementsToReserve),
755+
typename BaseT::ExactBucketCount{}) {}
756756

757757
DenseMap(const DenseMap &other) : DenseMap() { this->copyFrom(other); }
758758

@@ -784,7 +784,7 @@ class DenseMap : public DenseMapBase<DenseMap<KeyT, ValueT, KeyInfoT, BucketT>,
784784
DenseMap &operator=(DenseMap &&other) {
785785
this->destroyAll();
786786
deallocateBuckets();
787-
init(0);
787+
this->initWithExactBucketCount(0);
788788
this->swap(other);
789789
return *this;
790790
}
@@ -825,11 +825,6 @@ class DenseMap : public DenseMapBase<DenseMap<KeyT, ValueT, KeyInfoT, BucketT>,
825825
return true;
826826
}
827827

828-
void init(unsigned InitNumEntries) {
829-
auto InitBuckets = BaseT::getMinBucketToReserveForEntries(InitNumEntries);
830-
this->initWithExactBucketCount(InitBuckets);
831-
}
832-
833828
// Put the zombie instance in a known good state after a move.
834829
void kill() {
835830
deallocateBuckets();
@@ -898,9 +893,10 @@ class SmallDenseMap
898893
}
899894

900895
public:
901-
explicit SmallDenseMap(unsigned NumElementsToReserve = 0) {
902-
init(NumElementsToReserve);
903-
}
896+
explicit SmallDenseMap(unsigned NumElementsToReserve = 0)
897+
: SmallDenseMap(
898+
BaseT::getMinBucketToReserveForEntries(NumElementsToReserve),
899+
typename BaseT::ExactBucketCount{}) {}
904900

905901
SmallDenseMap(const SmallDenseMap &other) : SmallDenseMap() {
906902
this->copyFrom(other);
@@ -935,7 +931,7 @@ class SmallDenseMap
935931
SmallDenseMap &operator=(SmallDenseMap &&other) {
936932
this->destroyAll();
937933
deallocateBuckets();
938-
init(0);
934+
this->initWithExactBucketCount(0);
939935
this->swap(other);
940936
return *this;
941937
}
@@ -1091,11 +1087,6 @@ class SmallDenseMap
10911087
return true;
10921088
}
10931089

1094-
void init(unsigned InitNumEntries) {
1095-
auto InitBuckets = BaseT::getMinBucketToReserveForEntries(InitNumEntries);
1096-
this->initWithExactBucketCount(InitBuckets);
1097-
}
1098-
10991090
// Put the zombie instance in a known good state after a move.
11001091
void kill() {
11011092
deallocateBuckets();

0 commit comments

Comments
 (0)