[PATCH] rust: add fast path to `RBTree::find_mut` for empty tree

Onur Özkan posted 1 patch 3 months, 2 weeks ago
rust/kernel/rbtree.rs | 5 +++++
1 file changed, 5 insertions(+)
[PATCH] rust: add fast path to `RBTree::find_mut` for empty tree
Posted by Onur Özkan 3 months, 2 weeks ago
Adds a fast path to `RBTree::find_mut` that immediately returns `None`
when the tree is empty.

This should also benefit `RBTree::get_mut`, `RBTree::remove_node` and
`RBTree::remove` functions as they rely on `find_mut` internally.

Signed-off-by: Onur Özkan <work@onurozkan.dev>
---
 rust/kernel/rbtree.rs | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/rust/kernel/rbtree.rs b/rust/kernel/rbtree.rs
index 8d978c896747..a9b79b22ea32 100644
--- a/rust/kernel/rbtree.rs
+++ b/rust/kernel/rbtree.rs
@@ -365,6 +365,11 @@ pub fn entry(&mut self, key: K) -> Entry<'_, K, V> {

     /// Used for accessing the given node, if it exists.
     pub fn find_mut(&mut self, key: &K) -> Option<OccupiedEntry<'_, K, V>> {
+        // Fast path: return `None` when tree is empty.
+        if self.root.rb_node.is_null() {
+            return None;
+        }
+
         match self.raw_entry(key) {
             RawEntry::Occupied(entry) => Some(entry),
             RawEntry::Vacant(_entry) => None,
--
2.50.0