From e02aa8c8c9d36f2d45f91c462e3f55f5f39428e6 Mon Sep 17 00:00:00 2001
From: John Naylor <john.naylor@postgresql.org>
Date: Fri, 20 Jan 2023 14:55:25 +0700
Subject: [PATCH v21 08/22] Streamline calculation of slab blocksize

To reduce duplication. This will likely lead to
division instructions, but a few cycles won't
matter at all when creating the tree.
---
 src/include/lib/radixtree.h | 50 ++++++++++++++-----------------------
 1 file changed, 19 insertions(+), 31 deletions(-)

diff --git a/src/include/lib/radixtree.h b/src/include/lib/radixtree.h
index 0a39bd6664..172d62c6b0 100644
--- a/src/include/lib/radixtree.h
+++ b/src/include/lib/radixtree.h
@@ -304,6 +304,13 @@ RT_SCOPE void RT_STATS(RT_RADIX_TREE *tree);
 #define RT_NODE_KIND_256		0x03
 #define RT_NODE_KIND_COUNT		4
 
+/*
+ * Calculate the slab blocksize so that we can allocate at least 32 chunks
+ * from the block.
+ */
+#define RT_SLAB_BLOCK_SIZE(size)	\
+	Max((SLAB_DEFAULT_BLOCK_SIZE / (size)) * (size), (size) * 32)
+
 #endif							/* RT_COMMON */
 
 
@@ -503,59 +510,38 @@ typedef struct RT_SIZE_CLASS_ELEM
 	/* slab chunk size */
 	Size		inner_size;
 	Size		leaf_size;
-
-	/* slab block size */
-	Size		inner_blocksize;
-	Size		leaf_blocksize;
 } RT_SIZE_CLASS_ELEM;
 
-/*
- * Calculate the slab blocksize so that we can allocate at least 32 chunks
- * from the block.
- */
-#define NODE_SLAB_BLOCK_SIZE(size)	\
-	Max((SLAB_DEFAULT_BLOCK_SIZE / (size)) * (size), (size) * 32)
-
 static const RT_SIZE_CLASS_ELEM RT_SIZE_CLASS_INFO[] = {
 	[RT_CLASS_4_FULL] = {
 		.name = "radix tree node 4",
 		.fanout = 4,
 		.inner_size = sizeof(RT_NODE_INNER_4) + 4 * sizeof(RT_PTR_ALLOC),
 		.leaf_size = sizeof(RT_NODE_LEAF_4) + 4 * sizeof(RT_VALUE_TYPE),
-		.inner_blocksize = NODE_SLAB_BLOCK_SIZE(sizeof(RT_NODE_INNER_4) + 4 * sizeof(RT_PTR_ALLOC)),
-		.leaf_blocksize = NODE_SLAB_BLOCK_SIZE(sizeof(RT_NODE_LEAF_4) + 4 * sizeof(RT_VALUE_TYPE)),
 	},
 	[RT_CLASS_32_PARTIAL] = {
 		.name = "radix tree node 15",
 		.fanout = 15,
 		.inner_size = sizeof(RT_NODE_INNER_32) + 15 * sizeof(RT_PTR_ALLOC),
 		.leaf_size = sizeof(RT_NODE_LEAF_32) + 15 * sizeof(RT_VALUE_TYPE),
-		.inner_blocksize = NODE_SLAB_BLOCK_SIZE(sizeof(RT_NODE_INNER_32) + 15 * sizeof(RT_PTR_ALLOC)),
-		.leaf_blocksize = NODE_SLAB_BLOCK_SIZE(sizeof(RT_NODE_LEAF_32) + 15 * sizeof(RT_VALUE_TYPE)),
 	},
 	[RT_CLASS_32_FULL] = {
 		.name = "radix tree node 32",
 		.fanout = 32,
 		.inner_size = sizeof(RT_NODE_INNER_32) + 32 * sizeof(RT_PTR_ALLOC),
 		.leaf_size = sizeof(RT_NODE_LEAF_32) + 32 * sizeof(RT_VALUE_TYPE),
-		.inner_blocksize = NODE_SLAB_BLOCK_SIZE(sizeof(RT_NODE_INNER_32) + 32 * sizeof(RT_PTR_ALLOC)),
-		.leaf_blocksize = NODE_SLAB_BLOCK_SIZE(sizeof(RT_NODE_LEAF_32) + 32 * sizeof(RT_VALUE_TYPE)),
 	},
 	[RT_CLASS_125_FULL] = {
 		.name = "radix tree node 125",
 		.fanout = 125,
 		.inner_size = sizeof(RT_NODE_INNER_125) + 125 * sizeof(RT_PTR_ALLOC),
 		.leaf_size = sizeof(RT_NODE_LEAF_125) + 125 * sizeof(RT_VALUE_TYPE),
-		.inner_blocksize = NODE_SLAB_BLOCK_SIZE(sizeof(RT_NODE_INNER_125) + 125 * sizeof(RT_PTR_ALLOC)),
-		.leaf_blocksize = NODE_SLAB_BLOCK_SIZE(sizeof(RT_NODE_LEAF_125) + 125 * sizeof(RT_VALUE_TYPE)),
 	},
 	[RT_CLASS_256] = {
 		.name = "radix tree node 256",
 		.fanout = 256,
 		.inner_size = sizeof(RT_NODE_INNER_256),
 		.leaf_size = sizeof(RT_NODE_LEAF_256),
-		.inner_blocksize = NODE_SLAB_BLOCK_SIZE(sizeof(RT_NODE_INNER_256)),
-		.leaf_blocksize = NODE_SLAB_BLOCK_SIZE(sizeof(RT_NODE_LEAF_256)),
 	},
 };
 
@@ -1361,14 +1347,18 @@ RT_CREATE(MemoryContext ctx)
 	/* Create the slab allocator for each size class */
 	for (int i = 0; i < RT_SIZE_CLASS_COUNT; i++)
 	{
+		RT_SIZE_CLASS_ELEM size_class = RT_SIZE_CLASS_INFO[i];
+		size_t inner_blocksize = RT_SLAB_BLOCK_SIZE(size_class.inner_size);
+		size_t leaf_blocksize = RT_SLAB_BLOCK_SIZE(size_class.leaf_size);
+
 		tree->inner_slabs[i] = SlabContextCreate(ctx,
-												 RT_SIZE_CLASS_INFO[i].name,
-												 RT_SIZE_CLASS_INFO[i].inner_blocksize,
-												 RT_SIZE_CLASS_INFO[i].inner_size);
+												 size_class.name,
+												 inner_blocksize,
+												 size_class.inner_size);
 		tree->leaf_slabs[i] = SlabContextCreate(ctx,
-												RT_SIZE_CLASS_INFO[i].name,
-												RT_SIZE_CLASS_INFO[i].leaf_blocksize,
-												RT_SIZE_CLASS_INFO[i].leaf_size);
+												size_class.name,
+												leaf_blocksize,
+												size_class.leaf_size);
 	}
 #endif
 
@@ -2189,12 +2179,10 @@ RT_DUMP(RT_RADIX_TREE *tree)
 {
 
 	for (int i = 0; i < RT_SIZE_CLASS_COUNT; i++)
-		fprintf(stderr, "%s\tinner_size %zu\tinner_blocksize %zu\tleaf_size %zu\tleaf_blocksize %zu\n",
+		fprintf(stderr, "%s\tinner_size %zu\tleaf_size %zu\t%zu\n",
 				RT_SIZE_CLASS_INFO[i].name,
 				RT_SIZE_CLASS_INFO[i].inner_size,
-				RT_SIZE_CLASS_INFO[i].inner_blocksize,
-				RT_SIZE_CLASS_INFO[i].leaf_size,
-				RT_SIZE_CLASS_INFO[i].leaf_blocksize);
+				RT_SIZE_CLASS_INFO[i].leaf_size);
 	fprintf(stderr, "max_val = " UINT64_FORMAT "\n", tree->ctl->max_val);
 
 	if (!tree->ctl->root)
-- 
2.39.0

