From c76ee15aa409b6bb32f2038c4450f9b0263ad030 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=95=E4=B8=8E=E5=B0=86=E5=86=9B=E8=A7=A3=E6=88=98?= =?UTF-8?q?=E8=A2=8D?= <72246322+a1393323447@users.noreply.github.com> Date: Tue, 17 Jan 2023 13:30:45 +0800 Subject: [PATCH 1/3] replace 'if' with 'match' --- library/core/src/slice/mod.rs | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/library/core/src/slice/mod.rs b/library/core/src/slice/mod.rs index df7fe2bf76dcd..dc3e6ec38ebf3 100644 --- a/library/core/src/slice/mod.rs +++ b/library/core/src/slice/mod.rs @@ -2480,20 +2480,17 @@ impl [T] { // coupled with the `left + size <= self.len()` invariant means // we have `left + size/2 < self.len()`, and this is in-bounds. let cmp = f(unsafe { self.get_unchecked(mid) }); - - // The reason why we use if/else control flow rather than match - // is because match reorders comparison operations, which is perf sensitive. - // This is x86 asm for u8: https://rust.godbolt.org/z/8Y8Pra. - if cmp == Less { - left = mid + 1; - } else if cmp == Greater { - right = mid; - } else { - // SAFETY: same as the `get_unchecked` above - unsafe { crate::intrinsics::assume(mid < self.len()) }; - return Ok(mid); + + match cmp { + Less => left = mid + 1, + Greater => right = mid, + Equal => { + // SAFETY: same as the `get_unchecked` above + unsafe { crate::intrinsics::assume(mid < self.len()) }; + return Ok(mid); + } } - + size = right - left; } From b5155eefaf612effa43bfc8a10deefddcd48a8a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=95=E4=B8=8E=E5=B0=86=E5=86=9B=E8=A7=A3=E6=88=98?= =?UTF-8?q?=E8=A2=8D?= <72246322+a1393323447@users.noreply.github.com> Date: Tue, 17 Jan 2023 13:54:47 +0800 Subject: [PATCH 2/3] add `Ordering` Co-authored-by: Jubilee <46493976+workingjubilee@users.noreply.github.com> --- library/core/src/slice/mod.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/library/core/src/slice/mod.rs b/library/core/src/slice/mod.rs index dc3e6ec38ebf3..4fd30ecc20a4d 100644 --- a/library/core/src/slice/mod.rs +++ b/library/core/src/slice/mod.rs @@ -2482,9 +2482,9 @@ impl [T] { let cmp = f(unsafe { self.get_unchecked(mid) }); match cmp { - Less => left = mid + 1, - Greater => right = mid, - Equal => { + Ordering::Less => left = mid + 1, + Ordering::Greater => right = mid, + Ordering::Equal => { // SAFETY: same as the `get_unchecked` above unsafe { crate::intrinsics::assume(mid < self.len()) }; return Ok(mid); From aa2a968cdf279bb20de50a473cfce3b89996cfe2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=95=E4=B8=8E=E5=B0=86=E5=86=9B=E8=A7=A3=E6=88=98?= =?UTF-8?q?=E8=A2=8D?= <72246322+a1393323447@users.noreply.github.com> Date: Tue, 17 Jan 2023 14:05:53 +0800 Subject: [PATCH 3/3] remove extra whitespace --- library/core/src/slice/mod.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/library/core/src/slice/mod.rs b/library/core/src/slice/mod.rs index 4fd30ecc20a4d..c8987c7b4a88e 100644 --- a/library/core/src/slice/mod.rs +++ b/library/core/src/slice/mod.rs @@ -2480,7 +2480,7 @@ impl [T] { // coupled with the `left + size <= self.len()` invariant means // we have `left + size/2 < self.len()`, and this is in-bounds. let cmp = f(unsafe { self.get_unchecked(mid) }); - + match cmp { Ordering::Less => left = mid + 1, Ordering::Greater => right = mid, @@ -2490,7 +2490,7 @@ impl [T] { return Ok(mid); } } - + size = right - left; }