[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <f4d62504-140b-4fde-9b52-65cf3a0ddd0a@gmx.com>
Date: Fri, 13 Dec 2024 17:51:44 +1030
From: Qu Wenruo <quwenruo.btrfs@....com>
To: "Roger L. Beckermeyer III" <beckerlee3@...il.com>, dsterba@...e.cz,
peterz@...radead.org, oleg@...hat.com, mhiramat@...nel.org,
linux-kernel@...r.kernel.org
Cc: josef@...icpanda.com, linux-btrfs@...r.kernel.org, lkp@...el.com
Subject: Re: [PATCH 1/6] rbtree: add rb_find_add_cached() to rbtree.h
在 2024/12/13 03:16, Roger L. Beckermeyer III 写道:
> Adds rb_find_add_cached() as a helper function for use with
> red-black trees. Used in btrfs to reduce boilerplate code.
I won't call it boilerplate code though, it's just to utilize the cached
rb tree feature as an optimization.
And since rbtree is a tree-wide infrastructure, you need to be more
persuasive to add a new interface.
Yes, btrfs is utilizing this cached rb tree, but since you're adding a
new tree-wide interface, it will be much better to find another
driver/subsystem that can benefit from the new interface.
>
> Suggested-by: Josef Bacik <josef@...icpanda.com>
> Signed-off-by: Roger L. Beckermeyer III <beckerlee3@...il.com>
> ---
> include/linux/rbtree.h | 37 +++++++++++++++++++++++++++++++++++++
> 1 file changed, 37 insertions(+)
>
> diff --git a/include/linux/rbtree.h b/include/linux/rbtree.h
> index 7c173aa64e1e..0d4444c0cfb3 100644
> --- a/include/linux/rbtree.h
> +++ b/include/linux/rbtree.h
> @@ -210,6 +210,43 @@ rb_add(struct rb_node *node, struct rb_root *tree,
> rb_insert_color(node, tree);
> }
>
> +/**
> + * rb_find_add_cached() - find equivalent @node in @tree, or add @node
> + * @node: node to look-for / insert
> + * @tree: tree to search / modify
> + * @cmp: operator defining the node order
> + *
> + * Returns the rb_node matching @node, or NULL when no match is found and @node
> + * is inserted.
> + */
> +static __always_inline struct rb_node *
> +rb_find_add_cached(struct rb_node *node, struct rb_root_cached *tree,
> + int (*cmp)(struct rb_node *, const struct rb_node *))
This function is almost the same as rb_add_cached(), the only difference
is the extra handling for the cmp function returning 0.
So I'm wondering if it's possible to enhance rb_add_cached(), or even
refactor it so there can be a shared core function and rb_add_cached()
and rb_find_add_cached() can reuse the same function.
Thanks,
Qu
> +{
> + bool leftmost = true;
> + struct rb_node **link = &tree->rb_root.rb_node;
> + struct rb_node *parent = NULL;
> + int c;
> +
> + while (*link) {
> + parent = *link;
> + c = cmp(node, parent);
> +
> + if (c < 0) {
> + link = &parent->rb_left;
> + } else if (c > 0) {
> + link = &parent->rb_right;
> + leftmost = false;
> + } else {
> + return parent;
> + }
> + }
> +
> + rb_link_node(node, parent, link);
> + rb_insert_color_cached(node, tree, leftmost);
> + return NULL;
> +}
> +
> /**
> * rb_find_add() - find equivalent @node in @tree, or add @node
> * @node: node to look-for / insert
Powered by blists - more mailing lists