The Linux Kernel Mailing List
 help / color / mirror / Atom feed
From: Peter Zijlstra <peterz@infradead.org>
To: Qu Wenruo <quwenruo.btrfs@gmx.com>
Cc: "Roger L. Beckermeyer III" <beckerlee3@gmail.com>,
	dsterba@suse.cz, oleg@redhat.com, mhiramat@kernel.org,
	linux-kernel@vger.kernel.org, josef@toxicpanda.com,
	linux-btrfs@vger.kernel.org, lkp@intel.com
Subject: Re: [PATCH 1/6] rbtree: add rb_find_add_cached() to rbtree.h
Date: Fri, 13 Dec 2024 10:05:07 +0100	[thread overview]
Message-ID: <20241213090507.GB21636@noisy.programming.kicks-ass.net> (raw)
In-Reply-To: <f4d62504-140b-4fde-9b52-65cf3a0ddd0a@gmx.com>

On Fri, Dec 13, 2024 at 05:51:44PM +1030, Qu Wenruo wrote:
> 
> 
> 在 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.

Nah, all this is boilerplate :-)

> > 
> > Suggested-by: Josef Bacik <josef@toxicpanda.com>
> > Signed-off-by: Roger L. Beckermeyer III <beckerlee3@gmail.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.

Nope, rb_add_cached() can add multiple entries with the same key,
rb_find_add() cannot.

Also, note that all these things are effectively 'templates', they
generate code at the call site. The cmp() function as required for
find_add() is a tri-state return and generates more logic than the
binary less() required for add().


  reply	other threads:[~2024-12-13  9:05 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <cover.1733850317.git.beckerlee3@gmail.com>
2024-12-12 16:46 ` [PATCH 1/6] rbtree: add rb_find_add_cached() to rbtree.h Roger L. Beckermeyer III
2024-12-13  7:21   ` Qu Wenruo
2024-12-13  9:05     ` Peter Zijlstra [this message]
2024-12-13  9:06   ` Peter Zijlstra
2024-12-16 22:13     ` Qu Wenruo
2024-12-16 22:22       ` Peter Zijlstra
2024-12-16 22:40         ` Qu Wenruo

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20241213090507.GB21636@noisy.programming.kicks-ass.net \
    --to=peterz@infradead.org \
    --cc=beckerlee3@gmail.com \
    --cc=dsterba@suse.cz \
    --cc=josef@toxicpanda.com \
    --cc=linux-btrfs@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=lkp@intel.com \
    --cc=mhiramat@kernel.org \
    --cc=oleg@redhat.com \
    --cc=quwenruo.btrfs@gmx.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox