Skip to content

Commit

Permalink
rollup merge of rust-lang#21886: dotdash/fast_slice_iter
Browse files Browse the repository at this point in the history
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
  • Loading branch information
alexcrichton committed Feb 18, 2015
2 parents dfc5c0f + 7412d1b commit fa30c4f
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
7 changes: 6 additions & 1 deletion src/libcollections/vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -1587,8 +1588,12 @@ impl<T> AsSlice<T> for Vec<T> {
#[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
})
}
Expand Down
4 changes: 2 additions & 2 deletions src/libcore/ptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ impl<T> 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")]
Expand All @@ -330,7 +330,7 @@ impl<T> 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")]
Expand Down

0 comments on commit fa30c4f

Please sign in to comment.