Rust for Linux List
 help / color / mirror / Atom feed
* [PATCH] rust: rbtree: remove redundant comparison in find_best_match
@ 2026-11-21 13:16 Joel Fernandes
  2026-12-01 13:32 ` Alice Ryhl
  2026-12-01 13:46 ` Gary Guo
  0 siblings, 2 replies; 5+ messages in thread
From: Joel Fernandes @ 2026-11-21 13:16 UTC (permalink / raw)
  To: linux-kernel, Miguel Ojeda, Boqun Feng, Gary Guo,
	Björn Roy Baron, Benno Lossin, Andreas Hindborg, Alice Ryhl,
	Trevor Gross, Danilo Krummrich, Daniel Almeida, Tamir Duberstein,
	Alexandre Courbot, Onur Özkan
  Cc: Joel Fernandes, rust-for-linux

In the Less arm of find_best_match(), the current node was recorded as
the best match only if its key was smaller than the key of the best
match found so far. That test can never fail, so remove it. It saves a
comparison and reduces lines of code.

Signed-off-by: Joel Fernandes <jfern@kernel.org>
---
 rust/kernel/rbtree.rs | 12 ++----------
 1 file changed, 2 insertions(+), 10 deletions(-)

diff --git a/rust/kernel/rbtree.rs b/rust/kernel/rbtree.rs
index 6fbd579d4a43..5e64504d592e 100644
--- a/rust/kernel/rbtree.rs
+++ b/rust/kernel/rbtree.rs
@@ -493,7 +493,6 @@ pub fn cursor_lower_bound(&self, key: &K) -> Option<Cursor<'_, K, V>>
 
     fn find_best_match(&self, key: &K) -> Option<NonNull<bindings::rb_node>> {
         let mut node = self.root.rb_node;
-        let mut best_key: Option<&K> = None;
         let mut best_links: Option<NonNull<bindings::rb_node>> = None;
         while !node.is_null() {
             // SAFETY: By the type invariant of `Self`, all non-null `rb_node` pointers stored in `self`
@@ -515,15 +514,8 @@ fn find_best_match(&self, key: &K) -> Option<NonNull<bindings::rb_node>> {
                     node = node_ref.rb_right;
                 }
                 Ordering::Less => {
-                    let is_better_match = match best_key {
-                        None => true,
-                        Some(best) => best > this_key,
-                    };
-                    if is_better_match {
-                        best_key = Some(this_key);
-                        // SAFETY: `this` is a non-null node so it is valid by the type invariants.
-                        best_links = Some(unsafe { NonNull::new_unchecked(&mut (*this).links) });
-                    }
+                    // SAFETY: `this` is a non-null node so it is valid by the type invariants.
+                    best_links = Some(unsafe { NonNull::new_unchecked(&mut (*this).links) });
                     node = node_ref.rb_left;
                 }
             };
-- 
2.54.0


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH] rust: rbtree: remove redundant comparison in find_best_match
  2026-11-21 13:16 [PATCH] rust: rbtree: remove redundant comparison in find_best_match Joel Fernandes
@ 2026-12-01 13:32 ` Alice Ryhl
  2026-12-01 13:46 ` Gary Guo
  1 sibling, 0 replies; 5+ messages in thread
From: Alice Ryhl @ 2026-12-01 13:32 UTC (permalink / raw)
  To: Joel Fernandes
  Cc: Alice Ryhl, linux-kernel, Miguel Ojeda, Boqun Feng, Gary Guo,
	Björn Roy Baron, Benno Lossin, Andreas Hindborg,
	Trevor Gross, Danilo Krummrich, Daniel Almeida, Tamir Duberstein,
	Alexandre Courbot, Onur Özkan, rust-for-linux

On Sat, Nov 21, 2026 at 08:16:18AM -0500, Joel Fernandes wrote:
> In the Less arm of find_best_match(), the current node was recorded as
> the best match only if its key was smaller than the key of the best
> match found so far. That test can never fail, so remove it. It saves a
> comparison and reduces lines of code.
>
> Signed-off-by: Joel Fernandes <jfern@kernel.org>

Looks good to me! Since each `Ordering::Less` branch descends into the
left subtree, every subsequent candidate key is strictly smaller than the
previous best match by the BST invariant.

Reviewed-by: Alice Ryhl <aliceryhl@google.com>

Alice

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] rust: rbtree: remove redundant comparison in find_best_match
  2026-11-21 13:16 [PATCH] rust: rbtree: remove redundant comparison in find_best_match Joel Fernandes
  2026-12-01 13:32 ` Alice Ryhl
@ 2026-12-01 13:46 ` Gary Guo
  2026-12-01 14:22   ` Miguel Ojeda
  1 sibling, 1 reply; 5+ messages in thread
From: Gary Guo @ 2026-12-01 13:46 UTC (permalink / raw)
  To: Joel Fernandes, linux-kernel, Miguel Ojeda, Boqun Feng, Gary Guo,
	Björn Roy Baron, Benno Lossin, Andreas Hindborg, Alice Ryhl,
	Trevor Gross, Danilo Krummrich, Daniel Almeida, Tamir Duberstein,
	Alexandre Courbot, Onur Özkan
  Cc: rust-for-linux

On Sat Nov 21, 2026 at 1:16 PM GMT, Joel Fernandes wrote:
> In the Less arm of find_best_match(), the current node was recorded as
> the best match only if its key was smaller than the key of the best
> match found so far. That test can never fail, so remove it. It saves a
> comparison and reduces lines of code.

Can you put an one line summary of the above as comment in the code?

Best,
Gary

>
> Signed-off-by: Joel Fernandes <jfern@kernel.org>
> ---
>  rust/kernel/rbtree.rs | 12 ++----------
>  1 file changed, 2 insertions(+), 10 deletions(-)
>
> diff --git a/rust/kernel/rbtree.rs b/rust/kernel/rbtree.rs
> index 6fbd579d4a43..5e64504d592e 100644
> --- a/rust/kernel/rbtree.rs
> +++ b/rust/kernel/rbtree.rs
> @@ -493,7 +493,6 @@ pub fn cursor_lower_bound(&self, key: &K) -> Option<Cursor<'_, K, V>>
>  
>      fn find_best_match(&self, key: &K) -> Option<NonNull<bindings::rb_node>> {
>          let mut node = self.root.rb_node;
> -        let mut best_key: Option<&K> = None;
>          let mut best_links: Option<NonNull<bindings::rb_node>> = None;
>          while !node.is_null() {
>              // SAFETY: By the type invariant of `Self`, all non-null `rb_node` pointers stored in `self`
> @@ -515,15 +514,8 @@ fn find_best_match(&self, key: &K) -> Option<NonNull<bindings::rb_node>> {
>                      node = node_ref.rb_right;
>                  }
>                  Ordering::Less => {
> -                    let is_better_match = match best_key {
> -                        None => true,
> -                        Some(best) => best > this_key,
> -                    };
> -                    if is_better_match {
> -                        best_key = Some(this_key);
> -                        // SAFETY: `this` is a non-null node so it is valid by the type invariants.
> -                        best_links = Some(unsafe { NonNull::new_unchecked(&mut (*this).links) });
> -                    }
> +                    // SAFETY: `this` is a non-null node so it is valid by the type invariants.
> +                    best_links = Some(unsafe { NonNull::new_unchecked(&mut (*this).links) });
>                      node = node_ref.rb_left;
>                  }
>              };



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] rust: rbtree: remove redundant comparison in find_best_match
  2026-12-01 13:46 ` Gary Guo
@ 2026-12-01 14:22   ` Miguel Ojeda
  2026-12-01 19:50     ` Joel Fernandes
  0 siblings, 1 reply; 5+ messages in thread
From: Miguel Ojeda @ 2026-12-01 14:22 UTC (permalink / raw)
  To: Gary Guo
  Cc: Joel Fernandes, linux-kernel, Miguel Ojeda, Boqun Feng,
	Björn Roy Baron, Benno Lossin, Andreas Hindborg, Alice Ryhl,
	Trevor Gross, Danilo Krummrich, Daniel Almeida, Tamir Duberstein,
	Alexandre Courbot, Onur Özkan, rust-for-linux

On Tue, Dec 1, 2026 at 2:46 PM Gary Guo <gary@garyguo.net> wrote:
>
> Can you put an one line summary of the above as comment in the code?

+1, I also suggested adding a comment when I mentioned the patch to Alice.

Cheers,
Miguel

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] rust: rbtree: remove redundant comparison in find_best_match
  2026-12-01 14:22   ` Miguel Ojeda
@ 2026-12-01 19:50     ` Joel Fernandes
  0 siblings, 0 replies; 5+ messages in thread
From: Joel Fernandes @ 2026-12-01 19:50 UTC (permalink / raw)
  To: Miguel Ojeda
  Cc: Gary Guo, linux-kernel, Miguel Ojeda, Boqun Feng,
	Björn Roy Baron, Benno Lossin, Andreas Hindborg, Alice Ryhl,
	Trevor Gross, Danilo Krummrich, Daniel Almeida, Tamir Duberstein,
	Alexandre Courbot, Onur Özkan, rust-for-linux

On 12/01, Miguel Ojeda <miguel.ojeda.sandonis@gmail.com> wrote:
> On Tue, Dec 1, 2026 at 2:46 PM Gary Guo <gary@garyguo.net> wrote:
> >
> > Can you put an one line summary of the above as comment in the code?
> 
> +1, I also suggested adding a comment when I mentioned the patch to Alice.

Ok, I will add the comment and resend with Alice's tag.

thanks,

Joel Fernandes

> 
> Cheers,
> Miguel

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2026-12-01 19:50 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-21 13:16 [PATCH] rust: rbtree: remove redundant comparison in find_best_match Joel Fernandes
2026-12-01 13:32 ` Alice Ryhl
2026-12-01 13:46 ` Gary Guo
2026-12-01 14:22   ` Miguel Ojeda
2026-12-01 19:50     ` Joel Fernandes

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox