From 385ad48b3563fc3cb6fe4d98dfa746a4204ac092 Mon Sep 17 00:00:00 2001 From: Folyd Date: Thu, 4 Feb 2021 00:23:48 +0800 Subject: [PATCH] Explain why we use if/else control flow rather than match --- library/core/src/slice/mod.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/library/core/src/slice/mod.rs b/library/core/src/slice/mod.rs index c7dd000f71f3..bc3f02efdda6 100644 --- a/library/core/src/slice/mod.rs +++ b/library/core/src/slice/mod.rs @@ -2163,6 +2163,10 @@ impl [T] { // - `mid >= 0` // - `mid < size`: `mid` is limited by `[left; right)` bound. 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 {