From: Liam R. Howlett Date: Fri, 11 Jan 2019 02:01:25 +0000 (-0500) Subject: maple_tree: Avoid allocating a node in coalescing until necessary X-Git-Url: https://www.infradead.org/git/?a=commitdiff_plain;h=8f66e5c0aa3ea6410da4f2f938fccd94e50290ea;p=users%2Fjedix%2Flinux-maple.git maple_tree: Avoid allocating a node in coalescing until necessary When looping through a node, don't create a new node if there is no need to do so. Signed-off-by: Liam R. Howlett --- diff --git a/lib/maple_tree.c b/lib/maple_tree.c index c088620ffad7..13958fa73eb9 100644 --- a/lib/maple_tree.c +++ b/lib/maple_tree.c @@ -446,31 +446,47 @@ static int mas_coalesce(struct ma_state *mas) struct maple_range_64 *src = &mt_to_node(mas->node)->mr64; unsigned char s_slot, d_slot = 0; unsigned long last = mas->max; - struct maple_range_64 *dst; + struct maple_range_64 *dst = NULL; struct maple_node *mn; - /* Allocate a new node */ - mas_node_cnt(mas, 1); - if (mas_is_err(mas)) - return 0; - - mn = ma_next_alloc(mas); - dst = &mn->mr64; - mas->node = mt_mk_node(mn, maple_leaf_64); for (s_slot = 0; s_slot < MAPLE_RANGE64_SLOTS; s_slot++) { if (s_slot < MAPLE_RANGE64_SLOTS - 1) { - if (last == src->pivot[s_slot]) + if (last == src->pivot[s_slot]) { + if (!dst) { + int i = 0; + /* Allocate a new node */ + mas_node_cnt(mas, 1); + if (mas_is_err(mas)) + return 0; + + mn = ma_next_alloc(mas); + dst = &mn->mr64; + mn->parent = src->parent; + for (i = 0; i <= s_slot;i++) + { + RCU_INIT_POINTER(dst->slot[i], + src->slot[i]); + dst->pivot[i] = src->pivot[i]; + } + d_slot = s_slot; + mas->node = mt_mk_node(mn, + maple_leaf_64); + } continue; + } if (s_slot != 0 && src->pivot[s_slot] == 0) break; - dst->pivot[d_slot] = src->pivot[s_slot]; - } + last = src->pivot[s_slot]; - RCU_INIT_POINTER(dst->slot[d_slot], src->slot[s_slot]); - last = dst->pivot[d_slot++]; + if (dst) + dst->pivot[d_slot] = src->pivot[s_slot]; + } + if (dst) + RCU_INIT_POINTER(dst->slot[d_slot++], + src->slot[s_slot]); } return s_slot - d_slot; }