From 52b5150cfd3dfcdf518675e9073f03e061a63a53 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Steinbrink?= Date: Mon, 2 Feb 2015 21:45:49 +0100 Subject: [PATCH 1/2] Avoid ptrtoint when checking if a pointer is null Casting the pointer to an integer requires a ptrtoint, while casting 0 to a pointer is directly folded to a `null` value. --- src/libcore/ptr.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/libcore/ptr.rs b/src/libcore/ptr.rs index 072c60c7036cf..2779e67c74300 100644 --- a/src/libcore/ptr.rs +++ b/src/libcore/ptr.rs @@ -303,7 +303,7 @@ impl PtrExt for *const T { #[inline] #[stable(feature = "rust1", since = "1.0.0")] - fn is_null(self) -> bool { self as usize == 0 } + fn is_null(self) -> bool { self == 0 as *const T } #[inline] #[stable(feature = "rust1", since = "1.0.0")] @@ -330,7 +330,7 @@ impl PtrExt for *mut T { #[inline] #[stable(feature = "rust1", since = "1.0.0")] - fn is_null(self) -> bool { self as usize == 0 } + fn is_null(self) -> bool { self == 0 as *mut T } #[inline] #[stable(feature = "rust1", since = "1.0.0")] From 7412d1b2eff1c77241c54fa39508e7049d71ec7e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Steinbrink?= Date: Mon, 2 Feb 2015 19:25:44 +0100 Subject: [PATCH 2/2] Eliminate excessive null-checks from slice iterators The data pointer used in the slice is never null, using assume() to tell LLVM about it gets rid of various unneeded null checks when iterating over the slice. Since the snapshot compiler is still using an older LLVM version, omit the call in stage0, because compile times explode otherwise. Benchmarks from #18193 ```` running 5 tests test _range ... bench: 33329 ns/iter (+/- 417) test assembly ... bench: 33299 ns/iter (+/- 58) test enumerate ... bench: 33318 ns/iter (+/- 83) test iter ... bench: 33311 ns/iter (+/- 130) test position ... bench: 33300 ns/iter (+/- 47) test result: ok. 0 passed; 0 failed; 0 ignored; 5 measured ```` Fixes #18193 --- src/libcollections/vec.rs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/libcollections/vec.rs b/src/libcollections/vec.rs index bde733644b5b5..25226afd8c9b7 100644 --- a/src/libcollections/vec.rs +++ b/src/libcollections/vec.rs @@ -56,6 +56,7 @@ use core::cmp::{Ordering}; use core::default::Default; use core::fmt; use core::hash::{self, Hash}; +use core::intrinsics::assume; use core::iter::{repeat, FromIterator, IntoIterator}; use core::marker::{self, ContravariantLifetime, InvariantType}; use core::mem; @@ -1587,8 +1588,12 @@ impl AsSlice for Vec { #[stable(feature = "rust1", since = "1.0.0")] fn as_slice(&self) -> &[T] { unsafe { + let p = *self.ptr; + if cfg!(not(stage0)) { // NOTE remove cfg after next snapshot + assume(p != 0 as *mut T); + } mem::transmute(RawSlice { - data: *self.ptr, + data: p, len: self.len }) }