File tree 1 file changed +8
-9
lines changed
1 file changed +8
-9
lines changed Original file line number Diff line number Diff line change 6
6
7
7
#![ stable( feature = "rust1" , since = "1.0.0" ) ]
8
8
9
- use crate :: cmp:: Ordering :: { self , Greater , Less } ;
9
+ use crate :: cmp:: Ordering :: { self , Equal , Greater , Less } ;
10
10
use crate :: fmt;
11
11
use crate :: intrinsics:: { assert_unsafe_precondition, exact_div} ;
12
12
use crate :: marker:: Copy ;
@@ -2854,14 +2854,13 @@ impl<T> [T] {
2854
2854
// we have `left + size/2 < self.len()`, and this is in-bounds.
2855
2855
let cmp = f ( unsafe { self . get_unchecked ( mid) } ) ;
2856
2856
2857
- // The reason why we use if/else control flow rather than match
2858
- // is because match reorders comparison operations, which is perf sensitive.
2859
- // This is x86 asm for u8: https://rust.godbolt.org/z/8Y8Pra.
2860
- if cmp == Less {
2861
- left = mid + 1 ;
2862
- } else if cmp == Greater {
2863
- right = mid;
2864
- } else {
2857
+ // This control flow produces conditional moves, which results in
2858
+ // fewer branches and instructions than if/else or matching on
2859
+ // cmp::Ordering.
2860
+ // This is x86 asm for u8: https://rust.godbolt.org/z/698eYffTx.
2861
+ left = if cmp == Less { mid + 1 } else { left } ;
2862
+ right = if cmp == Greater { mid } else { right } ;
2863
+ if cmp == Equal {
2865
2864
// SAFETY: same as the `get_unchecked` above
2866
2865
unsafe { crate :: intrinsics:: assume ( mid < self . len ( ) ) } ;
2867
2866
return Ok ( mid) ;
You can’t perform that action at this time.
0 commit comments