[llvm] dcf8cd9 - [ADT] Consolidate the grow() logic in DenseMapBase (NFC) (#168316)
via llvm-commits
llvm-commits at lists.llvm.org
Mon Nov 17 08:00:06 PST 2025
Author: Kazu Hirata
Date: 2025-11-17T08:00:01-08:00
New Revision: dcf8cd9c5b7cf15a07850484c6bb50516c4faacd
URL: https://github.com/llvm/llvm-project/commit/dcf8cd9c5b7cf15a07850484c6bb50516c4faacd
DIFF: https://github.com/llvm/llvm-project/commit/dcf8cd9c5b7cf15a07850484c6bb50516c4faacd.diff
LOG: [ADT] Consolidate the grow() logic in DenseMapBase (NFC) (#168316)
This patch consolidates the grow() logic in DenseMapBase::grow.
With this patch, DenseMapBase::grow() creates a temporary grown
instance and then lets DenseMap/SmallDenseMap attempt to move the
instance back to *this. If it doesn't work, we move again.
The "attempt to move" always succeeds for DenseMap. For
SmallDenseMap, it succeeds only in the large mode.
This is part of the effort outlined in #168255.
Added:
Modified:
llvm/include/llvm/ADT/DenseMap.h
Removed:
################################################################################
diff --git a/llvm/include/llvm/ADT/DenseMap.h b/llvm/include/llvm/ADT/DenseMap.h
index aa5d8a8729647..887f1af2417c2 100644
--- a/llvm/include/llvm/ADT/DenseMap.h
+++ b/llvm/include/llvm/ADT/DenseMap.h
@@ -558,7 +558,12 @@ class DenseMapBase : public DebugEpochBase {
void grow(unsigned MinNumBuckets) {
unsigned NumBuckets = DerivedT::roundUpNumBuckets(MinNumBuckets);
- derived().grow(NumBuckets);
+ DerivedT Tmp(NumBuckets, ExactBucketCount{});
+ Tmp.moveFrom(derived());
+ if (derived().maybeMoveFast(std::move(Tmp)))
+ return;
+ initWithExactBucketCount(NumBuckets);
+ moveFrom(Tmp);
}
template <typename LookupKeyT>
@@ -842,10 +847,9 @@ class DenseMap : public DenseMapBase<DenseMap<KeyT, ValueT, KeyInfoT, BucketT>,
static_cast<unsigned>(NextPowerOf2(MinNumBuckets - 1)));
}
- void grow(unsigned AtLeast) {
- DenseMap Tmp(AtLeast, typename BaseT::ExactBucketCount{});
- Tmp.moveFrom(*this);
- swapImpl(Tmp);
+ bool maybeMoveFast(DenseMap &&Other) {
+ swapImpl(Other);
+ return true;
}
// Plan how to shrink the bucket table. Return:
@@ -1110,23 +1114,16 @@ class SmallDenseMap
static_cast<unsigned>(NextPowerOf2(MinNumBuckets - 1)));
}
- void grow(unsigned NumBuckets) {
- SmallDenseMap Tmp(NumBuckets, typename BaseT::ExactBucketCount{});
- Tmp.moveFrom(*this);
+ bool maybeMoveFast(SmallDenseMap &&Other) {
+ if (Other.Small)
+ return false;
- if (Tmp.Small) {
- // Use moveFrom in those rare cases where we stay in the small mode. This
- // can happen when we have many tombstones.
- Small = true;
- this->BaseT::initEmpty();
- this->moveFrom(Tmp);
- } else {
- Small = false;
- NumEntries = Tmp.NumEntries;
- NumTombstones = Tmp.NumTombstones;
- *getLargeRep() = std::move(*Tmp.getLargeRep());
- Tmp.getLargeRep()->NumBuckets = 0;
- }
+ Small = false;
+ NumEntries = Other.NumEntries;
+ NumTombstones = Other.NumTombstones;
+ *getLargeRep() = std::move(*Other.getLargeRep());
+ Other.getLargeRep()->NumBuckets = 0;
+ return true;
}
// Plan how to shrink the bucket table. Return:
More information about the llvm-commits
mailing list