Explain why we use if/else control flow rather than match

This commit is contained in:
Folyd 2021-02-04 00:23:48 +08:00
parent 7d078cfb94
commit 385ad48b35

View File

@ -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 {