Rust for Linux List
 help / color / mirror / Atom feed
From: "Gary Guo" <gary@garyguo.net>
To: "Joel Fernandes" <jfern@kernel.org>,
	linux-kernel@vger.kernel.org, "Miguel Ojeda" <ojeda@kernel.org>,
	"Boqun Feng" <boqun@kernel.org>, "Gary Guo" <gary@garyguo.net>,
	"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
	"Benno Lossin" <lossin@kernel.org>,
	"Andreas Hindborg" <a.hindborg@kernel.org>,
	"Alice Ryhl" <aliceryhl@google.com>,
	"Trevor Gross" <tmgross@umich.edu>,
	"Danilo Krummrich" <dakr@kernel.org>,
	"Daniel Almeida" <daniel.almeida@collabora.com>,
	"Tamir Duberstein" <tamird@kernel.org>,
	"Alexandre Courbot" <acourbot@nvidia.com>,
	"Onur Özkan" <work@onurozkan.dev>
Cc: <rust-for-linux@vger.kernel.org>
Subject: Re: [PATCH] rust: rbtree: remove redundant comparison in find_best_match
Date: Tue, 01 Dec 2026 13:46:47 +0000	[thread overview]
Message-ID: <DN9FXWVI8BZA.239XP1VOMW6YJ@garyguo.net> (raw)
In-Reply-To: <20261121131618.909529-1-jfern@kernel.org>

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;
>                  }
>              };



  parent reply	other threads:[~2026-12-01 13:46 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 [this message]
2026-12-01 14:22   ` Miguel Ojeda
2026-12-01 19:50     ` Joel Fernandes

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=DN9FXWVI8BZA.239XP1VOMW6YJ@garyguo.net \
    --to=gary@garyguo.net \
    --cc=a.hindborg@kernel.org \
    --cc=acourbot@nvidia.com \
    --cc=aliceryhl@google.com \
    --cc=bjorn3_gh@protonmail.com \
    --cc=boqun@kernel.org \
    --cc=dakr@kernel.org \
    --cc=daniel.almeida@collabora.com \
    --cc=jfern@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=lossin@kernel.org \
    --cc=ojeda@kernel.org \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=tamird@kernel.org \
    --cc=tmgross@umich.edu \
    --cc=work@onurozkan.dev \
    /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