[llvm] Avoid overhead of std::vector for free nodes (PR #196534)

Christoph Cullmann via llvm-commits llvm-commits at lists.llvm.org
Fri May 8 14:06:39 PDT 2026


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

>From 10f8374528b98d8c5db7e2c754d9a8aef12f0c34 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 | 14 +++++++++-----
 1 file changed, 9 insertions(+), 5 deletions(-)

diff --git a/llvm/include/llvm/ADT/ImmutableSet.h b/llvm/include/llvm/ADT/ImmutableSet.h
index b9c3e68e51365..552c81dcb12ea 100644
--- a/llvm/include/llvm/ADT/ImmutableSet.h
+++ b/llvm/include/llvm/ADT/ImmutableSet.h
@@ -342,7 +342,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;
   }
 };
 
@@ -368,7 +369,9 @@ class ImutAVLFactory {
   CacheTy Cache;
   uintptr_t Allocator;
   std::vector<TreeTy*> createdNodes;
-  std::vector<TreeTy*> freeNodes;
+
+  // We use next inside the TreeTy to build a single-linked freelist.
+  TreeTy *freeNodes = nullptr;
 
   bool ownsAllocator() const {
     return (Allocator & 0x1) == 0;
@@ -456,9 +459,10 @@ 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();
+    if (freeNodes) {
+      T = freeNodes;
+      freeNodes = freeNodes->next;
+      T->next = nullptr;
       assert(T != L);
       assert(T != R);
     } else {



More information about the llvm-commits mailing list