[llvm] [ADT] ImutAVLFactory: avoid overhead of vector for freelist (PR #196534)

Christoph Cullmann via llvm-commits llvm-commits at lists.llvm.org
Sat May 9 07:18:36 PDT 2026


https://github.com/christoph-cullmann updated https://github.com/llvm/llvm-project/pull/196534

>From b16552017ef335d8a18eb70063bab2a1f49b75d6 Mon Sep 17 00:00:00 2001
From: Christoph Cullmann <cullmann at kde.org>
Date: Fri, 8 May 2026 15:45:36 +0200
Subject: [PATCH] [ADT] ImutAVLFactory: avoid overhead of vector for freelist

we can use the next pointer of the TreeTy to have a zero overhead
freelist of all free nodes

avoids allocations in the destruction phase
---
 llvm/include/llvm/ADT/ImmutableSet.h | 21 +++++++++++----------
 1 file changed, 11 insertions(+), 10 deletions(-)

diff --git a/llvm/include/llvm/ADT/ImmutableSet.h b/llvm/include/llvm/ADT/ImmutableSet.h
index b9c3e68e51365..9917eb5be85b4 100644
--- a/llvm/include/llvm/ADT/ImmutableSet.h
+++ b/llvm/include/llvm/ADT/ImmutableSet.h
@@ -29,7 +29,6 @@
 #include <functional>
 #include <iterator>
 #include <new>
-#include <vector>
 
 namespace llvm {
 
@@ -342,7 +341,8 @@ class ImutAVLTree {
     // We need to clear the mutability bit in case we are
     // destroying the node as part of a sweep in ImutAVLFactory::recoverNodes().
     IsMutable = false;
-    factory->freeNodes.push_back(this);
+    next = factory->freeNodes;
+    factory->freeNodes = this;
   }
 };
 
@@ -367,8 +367,10 @@ class ImutAVLFactory {
 
   CacheTy Cache;
   uintptr_t Allocator;
-  std::vector<TreeTy*> createdNodes;
-  std::vector<TreeTy*> freeNodes;
+  SmallVector<TreeTy *, 0> createdNodes;
+
+  // We use next inside the TreeTy to build a single-linked freelist.
+  TreeTy *freeNodes = nullptr;
 
   bool ownsAllocator() const {
     return (Allocator & 0x1) == 0;
@@ -454,15 +456,14 @@ class ImutAVLFactory {
   //===--------------------------------------------------===//
 
   TreeTy* createNode(TreeTy* L, value_type_ref V, TreeTy* R) {
-    BumpPtrAllocator& A = getAllocator();
-    TreeTy* T;
-    if (!freeNodes.empty()) {
-      T = freeNodes.back();
-      freeNodes.pop_back();
+    TreeTy *T = freeNodes;
+    if (T) {
+      freeNodes = T->next;
+      T->next = nullptr;
       assert(T != L);
       assert(T != R);
     } else {
-      T = (TreeTy*) A.Allocate<TreeTy>();
+      T = (TreeTy *) getAllocator().Allocate<TreeTy>();
     }
     new (T) TreeTy(this, L, R, V, incrementHeight(L,R));
     createdNodes.push_back(T);



More information about the llvm-commits mailing list