[llvm] [ADT] Add computeNumBuckets to DenseMap (NFC) (PR #168301)

via llvm-commits llvm-commits at lists.llvm.org
Sun Nov 16 16:25:39 PST 2025


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-llvm-adt

Author: Kazu Hirata (kazutakahirata)

<details>
<summary>Changes</summary>

This patch adds computeNumBuckets, a helper function to compute the
number of buckets.

This is part of the effort outlined in #<!-- -->168255.  This makes it easier
to move the core logic of grow() to DenseMapBase::grow().


---
Full diff: https://github.com/llvm/llvm-project/pull/168301.diff


1 Files Affected:

- (modified) llvm/include/llvm/ADT/DenseMap.h (+15-5) 


``````````diff
diff --git a/llvm/include/llvm/ADT/DenseMap.h b/llvm/include/llvm/ADT/DenseMap.h
index 86592f12ce62c..152b6a237357d 100644
--- a/llvm/include/llvm/ADT/DenseMap.h
+++ b/llvm/include/llvm/ADT/DenseMap.h
@@ -556,7 +556,10 @@ class DenseMapBase : public DebugEpochBase {
     return llvm::make_range(getBuckets(), getBucketsEnd());
   }
 
-  void grow(unsigned AtLeast) { derived().grow(AtLeast); }
+  void grow(unsigned AtLeast) {
+    AtLeast = derived().computeNumBuckets(AtLeast);
+    derived().grow(AtLeast);
+  }
 
   template <typename LookupKeyT>
   BucketT *findBucketForInsertion(const LookupKeyT &Lookup,
@@ -840,8 +843,11 @@ class DenseMap : public DenseMapBase<DenseMap<KeyT, ValueT, KeyInfoT, BucketT>,
     NumBuckets = 0;
   }
 
+  static unsigned computeNumBuckets(unsigned NumBuckets) {
+    return std::max(64u, static_cast<unsigned>(NextPowerOf2(NumBuckets - 1)));
+  }
+
   void grow(unsigned AtLeast) {
-    AtLeast = std::max<unsigned>(64, NextPowerOf2(AtLeast - 1));
     DenseMap Tmp(AtLeast, typename BaseT::ExactBucketCount{});
     Tmp.moveFrom(*this);
     swapImpl(Tmp);
@@ -1106,10 +1112,14 @@ class SmallDenseMap
     new (getLargeRep()) LargeRep{nullptr, 0};
   }
 
-  void grow(unsigned AtLeast) {
-    if (AtLeast > InlineBuckets)
-      AtLeast = std::max<unsigned>(64, NextPowerOf2(AtLeast - 1));
+  static unsigned computeNumBuckets(unsigned NumBuckets) {
+    if (NumBuckets > InlineBuckets)
+      NumBuckets =
+          std::max(64u, static_cast<unsigned>(NextPowerOf2(NumBuckets - 1)));
+    return NumBuckets;
+  }
 
+  void grow(unsigned AtLeast) {
     SmallDenseMap Tmp(AtLeast, typename BaseT::ExactBucketCount{});
     Tmp.moveFrom(*this);
 

``````````

</details>


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


More information about the llvm-commits mailing list