File tree Expand file tree Collapse file tree 1 file changed +8
-9
lines changed Expand file tree Collapse file tree 1 file changed +8
-9
lines changed Original file line number Diff line number Diff line change 66
77#![ stable( feature = "rust1" , since = "1.0.0" ) ]
88
9- use crate :: cmp:: Ordering :: { self , Greater , Less } ;
9+ use crate :: cmp:: Ordering :: { self , Equal , Greater , Less } ;
1010use crate :: fmt;
1111use crate :: intrinsics:: { assert_unsafe_precondition, exact_div} ;
1212use crate :: marker:: Copy ;
@@ -2854,14 +2854,13 @@ impl<T> [T] {
28542854 // we have `left + size/2 < self.len()`, and this is in-bounds.
28552855 let cmp = f ( unsafe { self . get_unchecked ( mid) } ) ;
28562856
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 {
28652864 // SAFETY: same as the `get_unchecked` above
28662865 unsafe { crate :: intrinsics:: assume ( mid < self . len ( ) ) } ;
28672866 return Ok ( mid) ;
You can’t perform that action at this time.
0 commit comments