Skip to content

Commit

Permalink
rbtree: Add rb_add_augmented_cached() helper
Browse files Browse the repository at this point in the history
While slightly sub-optimal, updating the augmented data while going
down the tree during lookup would be faster -- alas the augment
interface does not currently allow for that, provide a generic helper
to add a node to an augmented cached tree.

Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Signed-off-by: Ingo Molnar <mingo@kernel.org>
Link: https://lore.kernel.org/r/20230531124603.862983648@infradead.org
  • Loading branch information
Peter Zijlstra authored and Ingo Molnar committed Jul 19, 2023
1 parent 86bfbb7 commit 99d4d26
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions include/linux/rbtree_augmented.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,32 @@ rb_insert_augmented_cached(struct rb_node *node,
rb_insert_augmented(node, &root->rb_root, augment);
}

static __always_inline struct rb_node *
rb_add_augmented_cached(struct rb_node *node, struct rb_root_cached *tree,
bool (*less)(struct rb_node *, const struct rb_node *),
const struct rb_augment_callbacks *augment)
{
struct rb_node **link = &tree->rb_root.rb_node;
struct rb_node *parent = NULL;
bool leftmost = true;

while (*link) {
parent = *link;
if (less(node, parent)) {
link = &parent->rb_left;
} else {
link = &parent->rb_right;
leftmost = false;
}
}

rb_link_node(node, parent, link);
augment->propagate(parent, NULL); /* suboptimal */
rb_insert_augmented_cached(node, tree, leftmost, augment);

return leftmost ? node : NULL;
}

/*
* Template for declaring augmented rbtree callbacks (generic case)
*
Expand Down

0 comments on commit 99d4d26

Please sign in to comment.