Re: [PATCH] rbtree: stop iteration early in rb_find_first

From: Peter Zijlstra
Date: Wed Aug 25 2021 - 07:39:43 EST


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@xxxxxxxxx>
> ---
> 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@xxxxxxxxxxxxx>