Skip to content

Commit d553d3b

Browse files
kazutakahiratamahesh-attarde
authored andcommitted
[ADT] Reduce code duplication in SmallDenseMap (NFC) (llvm#160813)
This patch reduces code duplication by having allocateBuckets take a larger role. Specifically, allocateBuckets now checks to see if we need to allocate heap memory and initializes Small appropriately. With this patch, allocateBuckets mirrors deallocateBuckets cleanly. Both methods handle the Small mode without asserting and are responsible for constructing and destructing LargeRep.
1 parent 0e63673 commit d553d3b

File tree

1 file changed

+13
-25
lines changed

1 file changed

+13
-25
lines changed

llvm/include/llvm/ADT/DenseMap.h

Lines changed: 13 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1009,21 +1009,13 @@ class SmallDenseMap
10091009
void copyFrom(const SmallDenseMap &other) {
10101010
this->destroyAll();
10111011
deallocateBuckets();
1012-
Small = true;
1013-
if (other.getNumBuckets() > InlineBuckets) {
1014-
Small = false;
1015-
new (getLargeRep()) LargeRep(allocateBuckets(other.getNumBuckets()));
1016-
}
1012+
allocateBuckets(other.getNumBuckets());
10171013
this->BaseT::copyFrom(other);
10181014
}
10191015

10201016
void init(unsigned InitNumEntries) {
10211017
auto InitBuckets = BaseT::getMinBucketToReserveForEntries(InitNumEntries);
1022-
Small = true;
1023-
if (InitBuckets > InlineBuckets) {
1024-
Small = false;
1025-
new (getLargeRep()) LargeRep(allocateBuckets(InitBuckets));
1026-
}
1018+
allocateBuckets(InitBuckets);
10271019
this->BaseT::initEmpty();
10281020
}
10291021

@@ -1057,21 +1049,14 @@ class SmallDenseMap
10571049
// AtLeast == InlineBuckets can happen if there are many tombstones,
10581050
// and grow() is used to remove them. Usually we always switch to the
10591051
// large rep here.
1060-
if (AtLeast > InlineBuckets) {
1061-
Small = false;
1062-
new (getLargeRep()) LargeRep(allocateBuckets(AtLeast));
1063-
}
1052+
allocateBuckets(AtLeast);
10641053
this->moveFromOldBuckets(llvm::make_range(TmpBegin, TmpEnd));
10651054
return;
10661055
}
10671056

10681057
LargeRep OldRep = std::move(*getLargeRep());
10691058
getLargeRep()->~LargeRep();
1070-
if (AtLeast <= InlineBuckets) {
1071-
Small = true;
1072-
} else {
1073-
new (getLargeRep()) LargeRep(allocateBuckets(AtLeast));
1074-
}
1059+
allocateBuckets(AtLeast);
10751060

10761061
this->moveFromOldBuckets(OldRep.buckets());
10771062

@@ -1166,12 +1151,15 @@ class SmallDenseMap
11661151
getLargeRep()->~LargeRep();
11671152
}
11681153

1169-
LargeRep allocateBuckets(unsigned Num) {
1170-
assert(Num > InlineBuckets && "Must allocate more buckets than are inline");
1171-
LargeRep Rep = {static_cast<BucketT *>(allocate_buffer(
1172-
sizeof(BucketT) * Num, alignof(BucketT))),
1173-
Num};
1174-
return Rep;
1154+
void allocateBuckets(unsigned Num) {
1155+
if (Num <= InlineBuckets) {
1156+
Small = true;
1157+
} else {
1158+
Small = false;
1159+
BucketT *NewBuckets = static_cast<BucketT *>(
1160+
allocate_buffer(sizeof(BucketT) * Num, alignof(BucketT)));
1161+
new (getLargeRep()) LargeRep{NewBuckets, Num};
1162+
}
11751163
}
11761164
};
11771165

0 commit comments

Comments
 (0)