lkml.org 
[lkml]   [2021]   [Aug]   [25]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
 
Messages in this thread
/
Date
From
SubjectRe: [PATCH] rbtree: stop iteration early in rb_find_first
On Wed, Aug 25, 2021 at 01:39:26PM +0200, Peter Zijlstra wrote:
> On Wed, Aug 25, 2021 at 05:59:48PM +0800, Li RongQing wrote:
> > stop iteration if match is not NULL and result of cmp is
> > not zero, this means the matched node has been found, and
> > the node with same key has been passed
> >
> > Signed-off-by: Li RongQing <lirongqing@baidu.com>
> > ---
> > include/linux/rbtree.h | 3 +++
> > 1 file changed, 3 insertions(+)
> >
> > diff --git a/include/linux/rbtree.h b/include/linux/rbtree.h
> > index d31ecaf4fdd3..2689771df9bb 100644
> > --- a/include/linux/rbtree.h
> > +++ b/include/linux/rbtree.h
> > @@ -324,6 +324,9 @@ rb_find_first(const void *key, const struct rb_root *tree,
> > } else if (c > 0) {
> > node = node->rb_right;
> > }
> > +
> > + if (match && c)
> > + break;
> > }
> >
> > return match;
>
> Acked-by: Peter Zijlstra (Intel) <peterz@infradead.org>

NAK. This looked slightly wrong before, and is more wrong after.

Before:
there was this weird condition if (c <= 0) {} else if (c > 0) {} ,
making you wonder what the third possibility may be. Easy fix would be
to remove the second condition.

After:
say the key is equal the root, so the code sets match=root and goes left.
Then it stops searching because match is set and c<0.
This doesn't work, the code needs to keep searching for the leftmost match.

--
Michel "walken" Lespinasse

\
 
 \ /
  Last update: 2021-08-25 13:53    [W:4.878 / U:0.044 seconds]
©2003-2020 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site