Skip to contentSkip to conversation

rust: rbtree: remove redundant comparison in find_best_match

PATCH New Joel Fernandes 1 participant 1 message Nov 21 Rust

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 rust: rbtree: remove redundant comparison in find_best_match

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.

---
 rust/kernel/rbtree.rs | 12 ++----------
 1 file changed, 2 insertions(+), 10 deletions(-)
rust/kernel/rbtree.rs +2 -10
index 6fbd579d4a43..5e64504d592e 100644
@@ -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;
                 }
             };
Signature · 1 line
2.54.0