Skip to content

Commit

Permalink
Eliminate excessive null-checks from slice iterators
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 #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
  • Loading branch information
dotdash committed Feb 18, 2015
1 parent 52b5150 commit 7412d1b
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/libcollections/vec.rs
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

0 comments on commit 7412d1b

Please sign in to comment.