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

* Re: [PATCH] rust: add fast path to `RBTree::find_mut` for empty tree
       [not found] ` <288841750859443@mail.yandex.com>
@ 2025-06-25 16:56   ` Miguel Ojeda
  0 siblings, 0 replies; 2+ messages in thread
From: Miguel Ojeda @ 2025-06-25 16:56 UTC (permalink / raw)
  To: Onur Özkan
  Cc: rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org,
	ojeda@kernel.org, alex.gaynor@gmail.com, boqun.feng@gmail.com,
	gary@garyguo.net, bjorn3_gh@protonmail.com, lossin@kernel.org,
	a.hindborg@kernel.org, aliceryhl@google.com, tmgross@umich.edu,
	dakr@kernel.org, mattgilbride@google.com, wedsonaf@gmail.com,
	tamird@gmail.com, daniel@sedlak.dev

On Wed, Jun 25, 2025 at 3:52 PM Onur Özkan <work@onurozkan.dev> wrote:
>
> Oops, I didn't realize that `rb_node` was a pointer. Please don't take the patch seriously.  :)

I think this reply didn't reach the lists because it uses HTML.

Cheers,
Miguel

^ permalink raw reply	[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).