Skip to content

Commit c3a4093

Browse files
[ADT] Consolidate copyFrom in DenseMap.h (NFC) (#165101)
DenseMap.h has: - DenseMapBase::copyFrom - DenseMap::copyFrom - SmallDenseMap::copyFrom The latter two clear and set up the storage again before delegating DenseMapBase::copyFrom to do the actual work of copying buckets. This patch consolidates all these into DenseMapBase::copyFrom while eliminating name shadowing concerns. Note that DenseMap::copyFrom and SmallDenseMap::copyFrom are nearly identical, and they can be made identical with small adjustments: - Set NumEntries and NumTombstones to 0 unconditionally. - Teach SmallDenseMap::allocateBuckets to always return true. This patch essentially applies these adjustments and then "inlines" the identical function body to the beginning of DenseMapBase::copyFrom. This patch de-templatizes DenseMapBase::copyFrom because nobody calls it with any type other than DerivedT.
1 parent e510797 commit c3a4093

File tree

1 file changed

+16
-26
lines changed

1 file changed

+16
-26
lines changed

llvm/include/llvm/ADT/DenseMap.h

Lines changed: 16 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -418,9 +418,16 @@ class DenseMapBase : public DebugEpochBase {
418418
}
419419
}
420420

421-
template <typename OtherBaseT>
422-
void copyFrom(
423-
const DenseMapBase<OtherBaseT, KeyT, ValueT, KeyInfoT, BucketT> &other) {
421+
void copyFrom(const DerivedT &other) {
422+
this->destroyAll();
423+
derived().deallocateBuckets();
424+
setNumEntries(0);
425+
setNumTombstones(0);
426+
if (!derived().allocateBuckets(other.getNumBuckets())) {
427+
// The bucket list is empty. No work to do.
428+
return;
429+
}
430+
424431
assert(&other != this);
425432
assert(getNumBuckets() == other.getNumBuckets());
426433

@@ -725,7 +732,7 @@ class DenseMap : public DenseMapBase<DenseMap<KeyT, ValueT, KeyInfoT, BucketT>,
725732

726733
DenseMap(const DenseMap &other) : BaseT() {
727734
init(0);
728-
copyFrom(other);
735+
this->copyFrom(other);
729736
}
730737

731738
DenseMap(DenseMap &&other) : BaseT() {
@@ -761,7 +768,7 @@ class DenseMap : public DenseMapBase<DenseMap<KeyT, ValueT, KeyInfoT, BucketT>,
761768

762769
DenseMap &operator=(const DenseMap &other) {
763770
if (&other != this)
764-
copyFrom(other);
771+
this->copyFrom(other);
765772
return *this;
766773
}
767774

@@ -831,17 +838,6 @@ class DenseMap : public DenseMapBase<DenseMap<KeyT, ValueT, KeyInfoT, BucketT>,
831838
}
832839
}
833840

834-
void copyFrom(const DenseMap &other) {
835-
this->destroyAll();
836-
deallocateBuckets();
837-
if (allocateBuckets(other.NumBuckets)) {
838-
this->BaseT::copyFrom(other);
839-
} else {
840-
NumEntries = 0;
841-
NumTombstones = 0;
842-
}
843-
}
844-
845841
void grow(unsigned AtLeast) {
846842
unsigned OldNumBuckets = NumBuckets;
847843
BucketT *OldBuckets = Buckets;
@@ -902,7 +898,7 @@ class SmallDenseMap
902898

903899
SmallDenseMap(const SmallDenseMap &other) : BaseT() {
904900
init(0);
905-
copyFrom(other);
901+
this->copyFrom(other);
906902
}
907903

908904
SmallDenseMap(SmallDenseMap &&other) : BaseT() {
@@ -1001,7 +997,7 @@ class SmallDenseMap
1001997

1002998
SmallDenseMap &operator=(const SmallDenseMap &other) {
1003999
if (&other != this)
1004-
copyFrom(other);
1000+
this->copyFrom(other);
10051001
return *this;
10061002
}
10071003

@@ -1099,7 +1095,7 @@ class SmallDenseMap
10991095
getLargeRep()->~LargeRep();
11001096
}
11011097

1102-
void allocateBuckets(unsigned Num) {
1098+
bool allocateBuckets(unsigned Num) {
11031099
if (Num <= InlineBuckets) {
11041100
Small = true;
11051101
} else {
@@ -1108,6 +1104,7 @@ class SmallDenseMap
11081104
allocate_buffer(sizeof(BucketT) * Num, alignof(BucketT)));
11091105
new (getLargeRep()) LargeRep{NewBuckets, Num};
11101106
}
1107+
return true;
11111108
}
11121109

11131110
void init(unsigned InitNumEntries) {
@@ -1116,13 +1113,6 @@ class SmallDenseMap
11161113
this->BaseT::initEmpty();
11171114
}
11181115

1119-
void copyFrom(const SmallDenseMap &other) {
1120-
this->destroyAll();
1121-
deallocateBuckets();
1122-
allocateBuckets(other.getNumBuckets());
1123-
this->BaseT::copyFrom(other);
1124-
}
1125-
11261116
void grow(unsigned AtLeast) {
11271117
if (AtLeast > InlineBuckets)
11281118
AtLeast = std::max<unsigned>(64, NextPowerOf2(AtLeast - 1));

0 commit comments

Comments
 (0)