about summary refs log tree commit diff
diff options
context:
space:
mode:
authorFolyd <lyshuhow@gmail.com>2021-02-04 00:23:48 +0800
committerFolyd <lyshuhow@gmail.com>2021-02-04 00:23:48 +0800
commit385ad48b3563fc3cb6fe4d98dfa746a4204ac092 (patch)
treee91d992b956c7dcbaf6c0d5e57e1988330e046fb
parent7d078cfb94fa75e5dee699535f3f9781d3a1d47d (diff)
downloadrust-385ad48b3563fc3cb6fe4d98dfa746a4204ac092.tar.gz
rust-385ad48b3563fc3cb6fe4d98dfa746a4204ac092.zip
Explain why we use if/else control flow rather than match
-rw-r--r--library/core/src/slice/mod.rs4
1 files changed, 4 insertions, 0 deletions
diff --git a/library/core/src/slice/mod.rs b/library/core/src/slice/mod.rs
index c7dd000f71f..bc3f02efdda 100644
--- a/library/core/src/slice/mod.rs
+++ b/library/core/src/slice/mod.rs
@@ -2163,6 +2163,10 @@ impl<T> [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 {