Skip to content

Commit

Permalink
Explain why we use if/else control flow rather than match
Browse files Browse the repository at this point in the history
  • Loading branch information
Folyd committed Feb 3, 2021
1 parent 7d078cf commit 385ad48
Showing 1 changed file with 4 additions and 0 deletions.
4 changes: 4 additions & 0 deletions library/core/src/slice/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 385ad48

Please sign in to comment.