Re: [RFC][PATCH 1/7] rbtree: Add generic add and find helpers

From: Peter Zijlstra
Date: Thu Apr 30 2020 - 05:27:16 EST


On Thu, Apr 30, 2020 at 10:46:17AM +0200, Peter Zijlstra wrote:
> On Wed, Apr 29, 2020 at 06:04:05PM -0700, Michel Lespinasse wrote:

> > - Have you considered passing a cmp() function to rb_add() and
> > rb_add_cached(), and having these test cmp() < 0 rather than less() ?
> > I figure every user will need to have a cmp() function, so it'd be
> > nicer if they didn't also need a less() function, if the generated
> > code is similar (if you checked and rejected it because of bad code,
> > please just say so).
>
> I did consider it; in fact I my original helpers had that.
>
> The reaosn I went with less() over cmp() is that the add() vs find()
> function signatures:
>
> bool (*less)(struct rb_node *, const struct rb_node *);
> int (*cmp)(const void *, const struct rb_node *);
>
> differ anyway in the left-hand argument; this is 'fixable' when you
> use an (on-stack) dummy object for find (as uprobes does), but that
> doesn't always work, esp. when the object is big. And given you need two
> functions anyway, I figured it was useful to name them differently.
>
> If you've looked at the other patches a bit, you'll see I've implemented
> both functions as 'trivial' wrappers around a single compare function in
> many cases.

I just realized I'd already done all this at some point ;-) See
rbtree_latch.h. Clearly I failed to realize the full potential back when
I did that.