Skip to content

Commit

Permalink
[ADT][NFC] Refactor/optimize DenseMap::copyFrom (llvm#108377)
Browse files Browse the repository at this point in the history
Simplify code, constexpr-ify if with constexpr condition
  • Loading branch information
marcauberer authored Sep 12, 2024
1 parent 8814b6d commit 853bb8f
Showing 1 changed file with 17 additions and 13 deletions.
30 changes: 17 additions & 13 deletions llvm/include/llvm/ADT/DenseMap.h
Original file line number Diff line number Diff line change
Expand Up @@ -471,19 +471,23 @@ class DenseMapBase : public DebugEpochBase {
setNumEntries(other.getNumEntries());
setNumTombstones(other.getNumTombstones());

if (std::is_trivially_copyable<KeyT>::value &&
std::is_trivially_copyable<ValueT>::value)
memcpy(reinterpret_cast<void *>(getBuckets()), other.getBuckets(),
getNumBuckets() * sizeof(BucketT));
else
for (size_t i = 0; i < getNumBuckets(); ++i) {
::new (&getBuckets()[i].getFirst())
KeyT(other.getBuckets()[i].getFirst());
if (!KeyInfoT::isEqual(getBuckets()[i].getFirst(), getEmptyKey()) &&
!KeyInfoT::isEqual(getBuckets()[i].getFirst(), getTombstoneKey()))
::new (&getBuckets()[i].getSecond())
ValueT(other.getBuckets()[i].getSecond());
BucketT *Buckets = getBuckets();
const BucketT *OtherBuckets = other.getBuckets();
const size_t NumBuckets = getNumBuckets();
if constexpr (std::is_trivially_copyable_v<KeyT> &&
std::is_trivially_copyable_v<ValueT>) {
memcpy(reinterpret_cast<void *>(Buckets), OtherBuckets,
NumBuckets * sizeof(BucketT));
} else {
const KeyT EmptyKey = getEmptyKey();
const KeyT TombstoneKey = getTombstoneKey();
for (size_t I = 0; I < NumBuckets; ++I) {
::new (&Buckets[I].getFirst()) KeyT(OtherBuckets[I].getFirst());
if (!KeyInfoT::isEqual(Buckets[I].getFirst(), EmptyKey) &&
!KeyInfoT::isEqual(Buckets[I].getFirst(), TombstoneKey))
::new (&Buckets[I].getSecond()) ValueT(OtherBuckets[I].getSecond());
}
}
}

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

static const KeyT getEmptyKey() {
static_assert(std::is_base_of<DenseMapBase, DerivedT>::value,
static_assert(std::is_base_of_v<DenseMapBase, DerivedT>,
"Must pass the derived type to this template!");
return KeyInfoT::getEmptyKey();
}
Expand Down

0 comments on commit 853bb8f

Please sign in to comment.