Skip to content

Commit fa30c4f

Browse files
committed
rollup merge of rust-lang#21886: dotdash/fast_slice_iter
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 rust-lang#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 rust-lang#18193
2 parents dfc5c0f + 7412d1b commit fa30c4f

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed

src/libcollections/vec.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ use core::cmp::{Ordering};
5656
use core::default::Default;
5757
use core::fmt;
5858
use core::hash::{self, Hash};
59+
use core::intrinsics::assume;
5960
use core::iter::{repeat, FromIterator, IntoIterator};
6061
use core::marker::{self, ContravariantLifetime, InvariantType};
6162
use core::mem;
@@ -1587,8 +1588,12 @@ impl<T> AsSlice<T> for Vec<T> {
15871588
#[stable(feature = "rust1", since = "1.0.0")]
15881589
fn as_slice(&self) -> &[T] {
15891590
unsafe {
1591+
let p = *self.ptr;
1592+
if cfg!(not(stage0)) { // NOTE remove cfg after next snapshot
1593+
assume(p != 0 as *mut T);
1594+
}
15901595
mem::transmute(RawSlice {
1591-
data: *self.ptr,
1596+
data: p,
15921597
len: self.len
15931598
})
15941599
}

src/libcore/ptr.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ impl<T> PtrExt for *const T {
303303

304304
#[inline]
305305
#[stable(feature = "rust1", since = "1.0.0")]
306-
fn is_null(self) -> bool { self as usize == 0 }
306+
fn is_null(self) -> bool { self == 0 as *const T }
307307

308308
#[inline]
309309
#[stable(feature = "rust1", since = "1.0.0")]
@@ -330,7 +330,7 @@ impl<T> PtrExt for *mut T {
330330

331331
#[inline]
332332
#[stable(feature = "rust1", since = "1.0.0")]
333-
fn is_null(self) -> bool { self as usize == 0 }
333+
fn is_null(self) -> bool { self == 0 as *mut T }
334334

335335
#[inline]
336336
#[stable(feature = "rust1", since = "1.0.0")]

0 commit comments

Comments
 (0)