Skip to content

Commit 853bb8f

Browse files
authored
[ADT][NFC] Refactor/optimize DenseMap::copyFrom (#108377)
Simplify code, constexpr-ify if with constexpr condition
1 parent 8814b6d commit 853bb8f

File tree

1 file changed

+17
-13
lines changed

1 file changed

+17
-13
lines changed

llvm/include/llvm/ADT/DenseMap.h

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -471,19 +471,23 @@ class DenseMapBase : public DebugEpochBase {
471471
setNumEntries(other.getNumEntries());
472472
setNumTombstones(other.getNumTombstones());
473473

474-
if (std::is_trivially_copyable<KeyT>::value &&
475-
std::is_trivially_copyable<ValueT>::value)
476-
memcpy(reinterpret_cast<void *>(getBuckets()), other.getBuckets(),
477-
getNumBuckets() * sizeof(BucketT));
478-
else
479-
for (size_t i = 0; i < getNumBuckets(); ++i) {
480-
::new (&getBuckets()[i].getFirst())
481-
KeyT(other.getBuckets()[i].getFirst());
482-
if (!KeyInfoT::isEqual(getBuckets()[i].getFirst(), getEmptyKey()) &&
483-
!KeyInfoT::isEqual(getBuckets()[i].getFirst(), getTombstoneKey()))
484-
::new (&getBuckets()[i].getSecond())
485-
ValueT(other.getBuckets()[i].getSecond());
474+
BucketT *Buckets = getBuckets();
475+
const BucketT *OtherBuckets = other.getBuckets();
476+
const size_t NumBuckets = getNumBuckets();
477+
if constexpr (std::is_trivially_copyable_v<KeyT> &&
478+
std::is_trivially_copyable_v<ValueT>) {
479+
memcpy(reinterpret_cast<void *>(Buckets), OtherBuckets,
480+
NumBuckets * sizeof(BucketT));
481+
} else {
482+
const KeyT EmptyKey = getEmptyKey();
483+
const KeyT TombstoneKey = getTombstoneKey();
484+
for (size_t I = 0; I < NumBuckets; ++I) {
485+
::new (&Buckets[I].getFirst()) KeyT(OtherBuckets[I].getFirst());
486+
if (!KeyInfoT::isEqual(Buckets[I].getFirst(), EmptyKey) &&
487+
!KeyInfoT::isEqual(Buckets[I].getFirst(), TombstoneKey))
488+
::new (&Buckets[I].getSecond()) ValueT(OtherBuckets[I].getSecond());
486489
}
490+
}
487491
}
488492

489493
static unsigned getHashValue(const KeyT &Val) {
@@ -496,7 +500,7 @@ class DenseMapBase : public DebugEpochBase {
496500
}
497501

498502
static const KeyT getEmptyKey() {
499-
static_assert(std::is_base_of<DenseMapBase, DerivedT>::value,
503+
static_assert(std::is_base_of_v<DenseMapBase, DerivedT>,
500504
"Must pass the derived type to this template!");
501505
return KeyInfoT::getEmptyKey();
502506
}

0 commit comments

Comments
 (0)