From 413cce02ce2419d4760f411a77f24213958ea906 Mon Sep 17 00:00:00 2001 From: John Naylor Date: Fri, 20 Jan 2023 11:32:24 +0700 Subject: [PATCH v22 05/22] Restore RT_GROW_NODE_KIND (This was previously "exploded" out during the work to switch this to a template) Change the API so that we pass it the allocated pointer and return the local pointer. That way, there is consistency in growing nodes whether we change kind or not. Also rename to RT_SWITCH_NODE_KIND, since it should work just as well for shrinking nodes. --- src/include/lib/radixtree.h | 104 +++--------------------- src/include/lib/radixtree_insert_impl.h | 24 ++---- 2 files changed, 19 insertions(+), 109 deletions(-) diff --git a/src/include/lib/radixtree.h b/src/include/lib/radixtree.h index a1458bc25f..c08016de3a 100644 --- a/src/include/lib/radixtree.h +++ b/src/include/lib/radixtree.h @@ -127,10 +127,9 @@ #define RT_ALLOC_NODE RT_MAKE_NAME(alloc_node) #define RT_INIT_NODE RT_MAKE_NAME(init_node) #define RT_FREE_NODE RT_MAKE_NAME(free_node) -#define RT_FREE_RECURSE RT_MAKE_NAME(free_recurse) #define RT_EXTEND RT_MAKE_NAME(extend) #define RT_SET_EXTEND RT_MAKE_NAME(set_extend) -//#define RT_GROW_NODE_KIND RT_MAKE_NAME(grow_node_kind) +#define RT_SWITCH_NODE_KIND RT_MAKE_NAME(grow_node_kind) #define RT_COPY_NODE RT_MAKE_NAME(copy_node) #define RT_REPLACE_NODE RT_MAKE_NAME(replace_node) #define RT_PTR_GET_LOCAL RT_MAKE_NAME(ptr_get_local) @@ -1080,26 +1079,22 @@ RT_COPY_NODE(RT_PTR_LOCAL newnode, RT_PTR_LOCAL oldnode) newnode->shift = oldnode->shift; newnode->count = oldnode->count; } -#if 0 + /* - * Create a new node with 'new_kind' and the same shift, chunk, and - * count of 'node'. + * Given a new allocated node and an old node, initalize the new + * node with the necessary fields and return its local pointer. */ -static RT_NODE* -RT_GROW_NODE_KIND(RT_RADIX_TREE *tree, RT_PTR_LOCAL node, uint8 new_kind) +static inline RT_PTR_LOCAL +RT_SWITCH_NODE_KIND(RT_RADIX_TREE *tree, RT_PTR_ALLOC allocnode, RT_PTR_LOCAL node, + uint8 new_kind, uint8 new_class, bool inner) { - RT_PTR_ALLOC allocnode; - RT_PTR_LOCAL newnode; - bool inner = !NODE_IS_LEAF(node); - - allocnode = RT_ALLOC_NODE(tree, RT_KIND_MIN_SIZE_CLASS[new_kind], inner); - newnode = RT_PTR_GET_LOCAL(tree, allocnode); - RT_INIT_NODE(newnode, new_kind, RT_KIND_MIN_SIZE_CLASS[new_kind], inner); + RT_PTR_LOCAL newnode = RT_PTR_GET_LOCAL(tree, allocnode); + RT_INIT_NODE(newnode, new_kind, new_class, inner); RT_COPY_NODE(newnode, node); return newnode; } -#endif + /* Free the given node */ static void RT_FREE_NODE(RT_RADIX_TREE *tree, RT_PTR_ALLOC allocnode) @@ -1415,78 +1410,6 @@ RT_GET_HANDLE(RT_RADIX_TREE *tree) Assert(tree->ctl->magic == RT_RADIX_TREE_MAGIC); return tree->ctl->handle; } - -/* - * Recursively free all nodes allocated to the DSA area. - */ -static inline void -RT_FREE_RECURSE(RT_RADIX_TREE *tree, RT_PTR_ALLOC ptr) -{ - RT_PTR_LOCAL node = RT_PTR_GET_LOCAL(tree, ptr); - - check_stack_depth(); - CHECK_FOR_INTERRUPTS(); - - /* The leaf node doesn't have child pointers */ - if (NODE_IS_LEAF(node)) - { - dsa_free(tree->dsa, ptr); - return; - } - - switch (node->kind) - { - case RT_NODE_KIND_4: - { - RT_NODE_INNER_4 *n4 = (RT_NODE_INNER_4 *) node; - - for (int i = 0; i < n4->base.n.count; i++) - RT_FREE_RECURSE(tree, n4->children[i]); - - break; - } - case RT_NODE_KIND_32: - { - RT_NODE_INNER_32 *n32 = (RT_NODE_INNER_32 *) node; - - for (int i = 0; i < n32->base.n.count; i++) - RT_FREE_RECURSE(tree, n32->children[i]); - - break; - } - case RT_NODE_KIND_125: - { - RT_NODE_INNER_125 *n125 = (RT_NODE_INNER_125 *) node; - - for (int i = 0; i < RT_NODE_MAX_SLOTS; i++) - { - if (!RT_NODE_125_IS_CHUNK_USED(&n125->base, i)) - continue; - - RT_FREE_RECURSE(tree, RT_NODE_INNER_125_GET_CHILD(n125, i)); - } - - break; - } - case RT_NODE_KIND_256: - { - RT_NODE_INNER_256 *n256 = (RT_NODE_INNER_256 *) node; - - for (int i = 0; i < RT_NODE_MAX_SLOTS; i++) - { - if (!RT_NODE_INNER_256_IS_CHUNK_USED(n256, i)) - continue; - - RT_FREE_RECURSE(tree, RT_NODE_INNER_256_GET_CHILD(n256, i)); - } - - break; - } - } - - /* Free the inner node */ - dsa_free(tree->dsa, ptr); -} #endif /* @@ -1498,10 +1421,6 @@ RT_FREE(RT_RADIX_TREE *tree) #ifdef RT_SHMEM Assert(tree->ctl->magic == RT_RADIX_TREE_MAGIC); - /* Free all memory used for radix tree nodes */ - if (RT_PTR_ALLOC_IS_VALID(tree->ctl->root)) - RT_FREE_RECURSE(tree, tree->ctl->root); - /* * Vandalize the control block to help catch programming error where * other backends access the memory formerly occupied by this radix tree. @@ -2280,10 +2199,9 @@ RT_DUMP(RT_RADIX_TREE *tree) #undef RT_ALLOC_NODE #undef RT_INIT_NODE #undef RT_FREE_NODE -#undef RT_FREE_RECURSE #undef RT_EXTEND #undef RT_SET_EXTEND -#undef RT_GROW_NODE_KIND +#undef RT_SWITCH_NODE_KIND #undef RT_COPY_NODE #undef RT_REPLACE_NODE #undef RT_PTR_GET_LOCAL diff --git a/src/include/lib/radixtree_insert_impl.h b/src/include/lib/radixtree_insert_impl.h index 1d0eb396e2..e3e44669ea 100644 --- a/src/include/lib/radixtree_insert_impl.h +++ b/src/include/lib/radixtree_insert_impl.h @@ -53,11 +53,9 @@ /* grow node from 4 to 32 */ allocnode = RT_ALLOC_NODE(tree, new_class, inner); - newnode = RT_PTR_GET_LOCAL(tree, allocnode); - RT_INIT_NODE(newnode, new_kind, new_class, inner); - RT_COPY_NODE(newnode, node); - //newnode = RT_GROW_NODE_KIND(tree, node, RT_NODE_KIND_32); + newnode = RT_SWITCH_NODE_KIND(tree, allocnode, node, new_kind, new_class, inner); new32 = (RT_NODE32_TYPE *) newnode; + #ifdef RT_NODE_LEVEL_LEAF RT_CHUNK_VALUES_ARRAY_COPY(n4->base.chunks, n4->values, new32->base.chunks, new32->values); @@ -119,13 +117,15 @@ if (unlikely(!VAR_NODE_HAS_FREE_SLOT(n32)) && n32->base.n.fanout == class32_min.fanout) { - /* grow to the next size class of this kind */ RT_PTR_ALLOC allocnode; RT_PTR_LOCAL newnode; const RT_SIZE_CLASS new_class = RT_CLASS_32_FULL; + /* grow to the next size class of this kind */ allocnode = RT_ALLOC_NODE(tree, new_class, inner); newnode = RT_PTR_GET_LOCAL(tree, allocnode); + n32 = (RT_NODE32_TYPE *) newnode; + #ifdef RT_NODE_LEVEL_LEAF memcpy(newnode, node, class32_min.leaf_size); #else @@ -135,9 +135,6 @@ RT_REPLACE_NODE(tree, parent, stored_node, node, allocnode, key); node = newnode; - - /* also update pointer for this kind */ - n32 = (RT_NODE32_TYPE *) newnode; } if (unlikely(!VAR_NODE_HAS_FREE_SLOT(n32))) @@ -152,10 +149,7 @@ /* grow node from 32 to 125 */ allocnode = RT_ALLOC_NODE(tree, new_class, inner); - newnode = RT_PTR_GET_LOCAL(tree, allocnode); - RT_INIT_NODE(newnode, new_kind, new_class, inner); - RT_COPY_NODE(newnode, node); - //newnode = RT_GROW_NODE_KIND(tree, node, RT_NODE_KIND_125); + newnode = RT_SWITCH_NODE_KIND(tree, allocnode, node, new_kind, new_class, inner); new125 = (RT_NODE125_TYPE *) newnode; for (int i = 0; i < class32_max.fanout; i++) @@ -229,11 +223,9 @@ /* grow node from 125 to 256 */ allocnode = RT_ALLOC_NODE(tree, new_class, inner); - newnode = RT_PTR_GET_LOCAL(tree, allocnode); - RT_INIT_NODE(newnode, new_kind, new_class, inner); - RT_COPY_NODE(newnode, node); - //newnode = RT_GROW_NODE_KIND(tree, node, RT_NODE_KIND_256); + newnode = RT_SWITCH_NODE_KIND(tree, allocnode, node, new_kind, new_class, inner); new256 = (RT_NODE256_TYPE *) newnode; + for (int i = 0; i < RT_NODE_MAX_SLOTS && cnt < n125->base.n.count; i++) { if (!RT_NODE_125_IS_CHUNK_USED(&n125->base, i)) -- 2.39.0