Skip to content

Commit d409475

Browse files
committed
Micro-optimize Ord::cmp for primitives
1 parent 65c53c3 commit d409475

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

library/core/src/cmp.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -1476,11 +1476,11 @@ mod impls {
14761476
impl const Ord for $t {
14771477
#[inline]
14781478
fn cmp(&self, other: &$t) -> Ordering {
1479-
// The order here is important to generate more optimal assembly.
1480-
// See <https://github.com/rust-lang/rust/issues/63758> for more info.
1481-
if *self < *other { Less }
1482-
else if *self == *other { Equal }
1483-
else { Greater }
1479+
let mut res = 0i8;
1480+
res -= (*self < *other) as i8;
1481+
res += (*self > *other) as i8;
1482+
// SAFETY: The discriminants of Ord were chosen to permit this
1483+
unsafe { crate::mem::transmute(res) }
14841484
}
14851485
}
14861486
)*)

0 commit comments

Comments
 (0)