[llvm] [ADT] Move the core logic of swapping to DenseMapBase::swap (NFC) (PR #168486)

Kazu Hirata via llvm-commits llvm-commits at lists.llvm.org
Tue Nov 18 08:56:11 PST 2025


================
@@ -363,7 +363,16 @@ class DenseMapBase : public DebugEpochBase {
   void swap(DerivedT &RHS) {
     this->incrementEpoch();
     RHS.incrementEpoch();
-    derived().swapImpl(RHS);
+    if (derived().maybeSwapFast(RHS.derived()))
+      return;
+
+    DerivedT &LHS = derived();
+    DerivedT Tmp;
+    Tmp.destroyAll();
----------------
kazutakahirata wrote:

In the `SmallDenseMap` case, `DerivedT Tmp;` would initialize each inline bucket with `EmptyKey`.  That needs to be destroyed.  Some unit tests do check that even empty keys are constructed and destructed correctly.

As an optimization, we could have a special constructor that does not initialize inline buckets.

FWIW, `destroyAll` is a no-op for trivially destructible key/value pairs:

```
  void destroyAll() {
    // No need to iterate through the buckets if both KeyT and ValueT are
    // trivially destructible.
    if constexpr (std::is_trivially_destructible_v<KeyT> &&
                  std::is_trivially_destructible_v<ValueT>)
      return;
```


https://github.com/llvm/llvm-project/pull/168486


More information about the llvm-commits mailing list