From 79d22b266f0df433dcb9147d0d64db53c681f6c7 Mon Sep 17 00:00:00 2001
From: John Naylor <john.naylor@postgresql.org>
Date: Fri, 20 Dec 2024 14:48:24 +0700
Subject: [PATCH v3 3/3] Always use the caller-provided context for radix tree
 leaves

Previously, RT_CREATE would create an additional slab context
if the values were fixed-length and larger than pointer size.
Commit XXXXX arranged so that the caller-provided context is only used for
leaves and nothing else, so if we override that choice, that context
will go completely unused. That's confusing, and we have no reason to
believe we know better than the developer, so just use what the caller
provided.

As demonstration, use slab in one of the regression tests
---
 src/include/lib/radixtree.h                      | 14 --------------
 src/test/modules/test_radixtree/test_radixtree.c |  5 +++--
 2 files changed, 3 insertions(+), 16 deletions(-)

diff --git a/src/include/lib/radixtree.h b/src/include/lib/radixtree.h
index 32f312305e..aa6354b2a4 100644
--- a/src/include/lib/radixtree.h
+++ b/src/include/lib/radixtree.h
@@ -1849,21 +1849,7 @@ RT_CREATE(MemoryContext ctx)
 												size_class.allocsize);
 	}
 
-	/* By default we use the passed context for leaves. */
 	tree->leaf_context = ctx;
-
-#ifndef RT_VARLEN_VALUE_SIZE
-
-	/*
-	 * For leaves storing fixed-length values, we use a slab context to avoid
-	 * the possibility of space wastage by power-of-2 rounding up.
-	 */
-	if (sizeof(RT_VALUE_TYPE) > sizeof(RT_PTR_ALLOC))
-		tree->leaf_context = SlabContextCreate(ctx,
-											   RT_STR(RT_PREFIX) "_radix_tree leaf context",
-											   RT_SLAB_BLOCK_SIZE(sizeof(RT_VALUE_TYPE)),
-											   sizeof(RT_VALUE_TYPE));
-#endif							/* !RT_VARLEN_VALUE_SIZE */
 #endif							/* RT_SHMEM */
 
 	/* add root node now so that RT_SET can assume it exists */
diff --git a/src/test/modules/test_radixtree/test_radixtree.c b/src/test/modules/test_radixtree/test_radixtree.c
index 8074b83695..5c54962edf 100644
--- a/src/test/modules/test_radixtree/test_radixtree.c
+++ b/src/test/modules/test_radixtree/test_radixtree.c
@@ -313,9 +313,10 @@ test_random(void)
 #else
 	MemoryContext radixtree_ctx;
 
-	radixtree_ctx = AllocSetContextCreate(CurrentMemoryContext,
+	radixtree_ctx = SlabContextCreate(CurrentMemoryContext,
 										  "test_radix_tree",
-										  ALLOCSET_SMALL_SIZES);
+										  SLAB_DEFAULT_BLOCK_SIZE,
+										  sizeof(TestValueType));
 	radixtree = rt_create(radixtree_ctx);
 #endif
 
-- 
2.47.1

