[llvm] [ADT] Clean up FoldingSet growth (NFC) (PR #217424)

via llvm-commits llvm-commits at lists.llvm.org
Wed Aug 19 11:23:16 PDT 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-llvm-support

Author: Kazu Hirata (kazutakahirata)

<details>
<summary>Changes</summary>

This patch cleans up hash table growth in FoldingSetBase:

- Remove GrowHashTable, a private 3-line wrapper around GrowBucketCount,
  in favor of calling GrowBucketCount(NumBuckets * 2, Info) directly in
  InsertNode.

- Refactor GrowBucketCount to use the RAII copy-and-swap pattern. Rehash
  nodes into a temporary FoldingSetBase and move-assign it to *this to
  avoid duplicating bucket allocation and deallocation logic.

Assisted-by: Antigravity


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


2 Files Affected:

- (modified) llvm/include/llvm/ADT/FoldingSet.h (-3) 
- (modified) llvm/lib/Support/FoldingSet.cpp (+10-20) 


``````````diff
diff --git a/llvm/include/llvm/ADT/FoldingSet.h b/llvm/include/llvm/ADT/FoldingSet.h
index 5e8bb9a87e64e..1b0e967c8308d 100644
--- a/llvm/include/llvm/ADT/FoldingSet.h
+++ b/llvm/include/llvm/ADT/FoldingSet.h
@@ -369,9 +369,6 @@ class FoldingSetBase {
   };
 
 private:
-  /// Double the size of the hash table and rehash everything.
-  void GrowHashTable(const FoldingSetInfo &Info);
-
   /// Resize the hash table and rehash everything. \p NewBucketCount must be a
   /// power of two, and must be greater than the old bucket count.
   void GrowBucketCount(unsigned NewBucketCount, const FoldingSetInfo &Info);
diff --git a/llvm/lib/Support/FoldingSet.cpp b/llvm/lib/Support/FoldingSet.cpp
index f0d94ae2d5a80..1423bc51b78b6 100644
--- a/llvm/lib/Support/FoldingSet.cpp
+++ b/llvm/lib/Support/FoldingSet.cpp
@@ -219,18 +219,11 @@ void FoldingSetBase::GrowBucketCount(unsigned NewBucketCount,
   assert((NewBucketCount > NumBuckets) &&
          "Can't shrink a folding set with GrowBucketCount");
   assert(isPowerOf2_32(NewBucketCount) && "Bad bucket count!");
-  void **OldBuckets = Buckets;
-  unsigned OldNumBuckets = NumBuckets;
 
-  // Clear out new buckets.
-  Buckets = AllocateBuckets(NewBucketCount);
-  NumBuckets = NewBucketCount;
-  NumNodes = 0;
-
-  // Walk the old buckets, rehashing nodes into their new place.
+  FoldingSetBase Tmp(llvm::Log2_32(NewBucketCount));
   FoldingSetNodeID TempID;
-  for (unsigned i = 0; i != OldNumBuckets; ++i) {
-    void *Probe = OldBuckets[i];
+  for (unsigned i = 0; i != NumBuckets; ++i) {
+    void *Probe = Buckets[i];
     if (!Probe)
       continue;
     while (Node *NodeInBucket = GetNextPtr(Probe)) {
@@ -239,19 +232,16 @@ void FoldingSetBase::GrowBucketCount(unsigned NewBucketCount,
       NodeInBucket->SetNextInBucket(nullptr);
 
       // Insert the node into the new bucket, after recomputing the hash.
-      InsertNode(NodeInBucket,
-                 GetBucketFor(Info.ComputeNodeHash(this, NodeInBucket, TempID),
-                              Buckets, NumBuckets),
-                 Info);
+      Tmp.InsertNode(
+          NodeInBucket,
+          GetBucketFor(Info.ComputeNodeHash(this, NodeInBucket, TempID),
+                       Tmp.Buckets, Tmp.NumBuckets),
+          Info);
       TempID.clear();
     }
   }
 
-  free(OldBuckets);
-}
-
-void FoldingSetBase::GrowHashTable(const FoldingSetInfo &Info) {
-  GrowBucketCount(NumBuckets * 2, Info);
+  *this = std::move(Tmp);
 }
 
 void FoldingSetBase::reserve(unsigned EltCount, const FoldingSetInfo &Info) {
@@ -290,7 +280,7 @@ void FoldingSetBase::InsertNode(Node *N, void *InsertPos,
   assert(!N->getNextInBucket());
   // Do we need to grow the hashtable?
   if (NumNodes + 1 > capacity()) {
-    GrowHashTable(Info);
+    GrowBucketCount(NumBuckets * 2, Info);
     FoldingSetNodeID TempID;
     InsertPos = GetBucketFor(Info.ComputeNodeHash(this, N, TempID), Buckets,
                              NumBuckets);

``````````

</details>


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


More information about the llvm-commits mailing list