From 4ca769ad091ef0018f5a20effaf4b4f428a034d7 Mon Sep 17 00:00:00 2001 From: Krishna Sai Veera Reddy Date: Fri, 29 Nov 2019 15:22:44 -0700 Subject: Optimize Ord trait implementation for bool Casting the booleans to `i8`s and converting their difference into `Ordering` generates better assembly than casting them to `u8`s and comparing them. --- src/libcore/cmp.rs | 11 ++++++++++- src/libcore/tests/cmp.rs | 8 ++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) (limited to 'src/libcore') diff --git a/src/libcore/cmp.rs b/src/libcore/cmp.rs index eea3dc39d34..e72b6117ba8 100644 --- a/src/libcore/cmp.rs +++ b/src/libcore/cmp.rs @@ -1006,6 +1006,7 @@ pub fn max_by_key K, K: Ord>(v1: T, v2: T, mut f: F) -> T { // Implementation of PartialEq, Eq, PartialOrd and Ord for primitive types mod impls { + use crate::hint::unreachable_unchecked; use crate::cmp::Ordering::{self, Less, Greater, Equal}; macro_rules! partial_eq_impl { @@ -1126,7 +1127,15 @@ mod impls { impl Ord for bool { #[inline] fn cmp(&self, other: &bool) -> Ordering { - (*self as u8).cmp(&(*other as u8)) + // Casting to i8's and converting the difference to an Ordering generates + // more optimal assembly. + // See for more info. + match (*self as i8) - (*other as i8) { + -1 => Less, + 0 => Equal, + 1 => Greater, + _ => unsafe { unreachable_unchecked() }, + } } } diff --git a/src/libcore/tests/cmp.rs b/src/libcore/tests/cmp.rs index 5e6778e222a..56a2f4acf6e 100644 --- a/src/libcore/tests/cmp.rs +++ b/src/libcore/tests/cmp.rs @@ -9,6 +9,14 @@ fn test_int_totalord() { assert_eq!(12.cmp(&-5), Greater); } +#[test] +fn test_bool_totalord() { + assert_eq!(true.cmp(&false), Greater); + assert_eq!(false.cmp(&true), Less); + assert_eq!(true.cmp(&true), Equal); + assert_eq!(false.cmp(&false), Equal); +} + #[test] fn test_mut_int_totalord() { assert_eq!((&mut 5).cmp(&&mut 10), Less); -- cgit 1.4.1-3-g733a5 From a30ee8e7636d2cf26eba89a8402cdfefebf9845e Mon Sep 17 00:00:00 2001 From: Krishna Sai Veera Reddy Date: Mon, 2 Dec 2019 08:45:35 -0700 Subject: Document usage of unsafe block --- src/libcore/cmp.rs | 1 + 1 file changed, 1 insertion(+) (limited to 'src/libcore') diff --git a/src/libcore/cmp.rs b/src/libcore/cmp.rs index e72b6117ba8..e16a428feb6 100644 --- a/src/libcore/cmp.rs +++ b/src/libcore/cmp.rs @@ -1134,6 +1134,7 @@ mod impls { -1 => Less, 0 => Equal, 1 => Greater, + // SAFETY: Unreachable code _ => unsafe { unreachable_unchecked() }, } } -- cgit 1.4.1-3-g733a5 From a983e0590a43ed8b0f60417828efd4e79b51f494 Mon Sep 17 00:00:00 2001 From: Matthew Kraai Date: Mon, 9 Dec 2019 06:49:37 -0800 Subject: Remove `checked_add` in `Layout::repeat` --- src/libcore/alloc.rs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) (limited to 'src/libcore') diff --git a/src/libcore/alloc.rs b/src/libcore/alloc.rs index 20248f7f6c1..3ebdb916f99 100644 --- a/src/libcore/alloc.rs +++ b/src/libcore/alloc.rs @@ -239,8 +239,11 @@ impl Layout { #[unstable(feature = "alloc_layout_extra", issue = "55724")] #[inline] pub fn repeat(&self, n: usize) -> Result<(Self, usize), LayoutErr> { - let padded_size = self.size().checked_add(self.padding_needed_for(self.align())) - .ok_or(LayoutErr { private: () })?; + // This cannot overflow. Quoting from the invariant of Layout: + // > `size`, when rounded up to the nearest multiple of `align`, + // > must not overflow (i.e., the rounded value must be less than + // > `usize::MAX`) + let padded_size = self.size() + self.padding_needed_for(self.align()); let alloc_size = padded_size.checked_mul(n) .ok_or(LayoutErr { private: () })?; -- cgit 1.4.1-3-g733a5 From 1f07aa582a41e6fc253139909d3bf9bfd04a9d6d Mon Sep 17 00:00:00 2001 From: Krishna Sai Veera Reddy Date: Tue, 10 Dec 2019 14:30:06 -0700 Subject: Add better documentation for unsafe block --- src/libcore/cmp.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/libcore') diff --git a/src/libcore/cmp.rs b/src/libcore/cmp.rs index e16a428feb6..87a407c5e9f 100644 --- a/src/libcore/cmp.rs +++ b/src/libcore/cmp.rs @@ -1134,7 +1134,7 @@ mod impls { -1 => Less, 0 => Equal, 1 => Greater, - // SAFETY: Unreachable code + // SAFETY: bool as i8 returns 0 or 1, so the difference can't be anything else _ => unsafe { unreachable_unchecked() }, } } -- cgit 1.4.1-3-g733a5