rust-for-linux.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] rust: add fast path to `RBTree::find_mut` for empty tree
@ 2025-06-25 13:42 Onur Özkan
       [not found] ` <288841750859443@mail.yandex.com>
  0 siblings, 1 reply; 2+ messages in thread
From: Onur Özkan @ 2025-06-25 13:42 UTC (permalink / raw)
  To: rust-for-linux, linux-kernel
  Cc: ojeda, alex.gaynor, boqun.feng, gary, bjorn3_gh, lossin,
	a.hindborg, aliceryhl, tmgross, dakr, mattgilbride, wedsonaf,
	tamird, daniel, Onur Özkan

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


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

end of thread, other threads:[~2025-06-25 16:56 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-06-25 13:42 [PATCH] rust: add fast path to `RBTree::find_mut` for empty tree Onur Özkan
     [not found] ` <288841750859443@mail.yandex.com>
2025-06-25 16:56   ` Miguel Ojeda

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).