From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796155135; b=BFy/5mSU+6bzFJuSLUkqKbISPHYwppa3ITQpXEhdcLIYUWsHXSRZoYe+AFwI0MMwsxFv eCpFzEDBy+5GQytppM5GTU5Q0xOCD2WU14HSG8qhhFdfspdc3pB5PxcAajf0khtHMLzA+ JaTytcygobyWZkD+gBy9FVALXfFcRO/vNDbQvOGD5owezMUlJCeBKfy+HLGVMII1t/wo2 FsgUcZhZRyczuIZ+u5LZ7af7ZcjgTXDEcARzIKtsLODmk2tTb5RZk1ZIPXRZXqHrI6Qox 5kvHx7fZlQJLbOylGdlkudXmdrNOq9Ku+b6frWYI4wi++Je5WgwwndihZCCnQGnhMHA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796155135; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: MIME-Version:Content-Transfer-Encoding; bh=IBsdw042TwdwMMCXsiAJIOgGI/BJqZTt/1yVWIuT8yY=; b=L21HFutw8ULzDBoJ7slt/NoA2ZabrUmOuEfpyJ9z02rvOA3F9PPRvqLEFy52p1UABBBE 3TnLGpRj7N5s91q3CTYheO+C4BUud1HEkeDID5kxU5++Jt4NZea/IzSEEZ4LwgOp6Y1Di 2tpBXQsiJH8tzHlrEJBGpi0qFabfXpGfe56SjbR3AgEjXpah8LmR9mpNHsiH4f/apPP+Q jTm5Xoc+Wc3zlwjmNUu4rtrtSvgMFnBeOXdm2wevD6t3/7Hxr4b417/Bj67F3++Ppv4oK oChgQB70S84hronsXt82UoXmqYmlXqFZQDCvPu/TVUsNKPuqasVxdMTf/nYo3PjX0QQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=ogQSAyJj; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with UTF8SMTPS id D3D6F606A31A; Tue, 1 Dec 2026 19:58:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=ogQSAyJj Received: by smtp.kernel.org (Postfix) with UTF8SMTPSA id 4D2D61F000C1; Tue, 1 Dec 2026 19:58:55 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796155135; bh=IBsdw042TwdwMMCXsiAJIOgGI/BJqZTt/1yVWIuT8yY=; h=From:To:Cc:Subject:Date; b=ogQSAyJjPiXu9UH/onHxPTE368t76Sv6AOjmTdlnvMg++5SzieALzhMVjy31Ylz78 qZNSRK/R8h+onkoC4NZMfEA3CY/ydlNOlN7KePG2jJu/zvTFakoscX9Gy+ZejD7Sq7 ppfwrWIx2wUzJO/iYP2kJAndgAANvFQ685xLVS4m/5NLEgNDNAk6zU2bhiRPTA1oHD x2dClCOti7MUwym4zBgrpX5n48RdxM3kNZM/f6/t+DYYpnBqCjwWe081VXAgMYVVXi UoVA44bp91+TNxv2gizv2SQ723nLQ54UDgXGKjnw7q9zoUc6OYrDwdIC5LY11PqWgo 8SRZLz5/iKY1w== From: Joel Fernandes To: linux-kernel@vger.kernel.org, Miguel Ojeda , Boqun Feng , Gary Guo , =?UTF-8?q?Bj=C3=B6rn=20Roy=20Baron?= , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross , Danilo Krummrich , Daniel Almeida , Tamir Duberstein , Alexandre Courbot , =?UTF-8?q?Onur=20=C3=96zkan?= Cc: Joel Fernandes , rust-for-linux@vger.kernel.org Subject: [PATCH v2] rust: rbtree: remove redundant comparison in find_best_match Date: Tue, 1 Dec 2026 14:58:47 -0500 Message-ID: <20261201195847.3692425-1-jfern@kernel.org> X-Mailer: git-send-email 2.54.0 X-Mailing-List: rust-for-linux@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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. Reviewed-by: Alice Ryhl Signed-off-by: Joel Fernandes --- Changes in v2: - Add a one-line comment explaining why. (Gary, Miguel) - Collect Alice's Reviewed-by. v1: https://lore.kernel.org/all/20261121131618.909529-1-jfern@kernel.org/ rust/kernel/rbtree.rs | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/rust/kernel/rbtree.rs b/rust/kernel/rbtree.rs index 6fbd579d4a43..662485efa8e0 100644 --- a/rust/kernel/rbtree.rs +++ b/rust/kernel/rbtree.rs @@ -493,7 +493,6 @@ pub fn cursor_lower_bound(&self, key: &K) -> Option> fn find_best_match(&self, key: &K) -> Option> { let mut node = self.root.rb_node; - let mut best_key: Option<&K> = None; let mut best_links: Option> = None; while !node.is_null() { // SAFETY: By the type invariant of `Self`, all non-null `rb_node` pointers stored in `self` @@ -515,15 +514,9 @@ fn find_best_match(&self, key: &K) -> Option> { 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) }); - } + // Keys shrink as we go left, so no comparison with the previous best is needed. + // 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; } }; base-commit: 18b332441bbf59fa537929fe390238e137a6caff -- 2.54.0