Skip to content

Commit

Permalink
Micro-optimize Ord::cmp for primitives
Browse files Browse the repository at this point in the history
  • Loading branch information
saethlin committed Dec 17, 2022
1 parent 65c53c3 commit d409475
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions library/core/src/cmp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1476,11 +1476,11 @@ mod impls {
impl const Ord for $t {
#[inline]
fn cmp(&self, other: &$t) -> Ordering {
// The order here is important to generate more optimal assembly.
// See <https://github.com/rust-lang/rust/issues/63758> for more info.
if *self < *other { Less }
else if *self == *other { Equal }
else { Greater }
let mut res = 0i8;
res -= (*self < *other) as i8;
res += (*self > *other) as i8;
// SAFETY: The discriminants of Ord were chosen to permit this
unsafe { crate::mem::transmute(res) }
}
}
)*)
Expand Down

0 comments on commit d409475

Please sign in to comment.