Skip to content

Commit

Permalink
Auto merge of #52206 - RalfJung:zst-slices, r=<try>
Browse files Browse the repository at this point in the history
slices: fix ZST slice iterators making up pointers; debug_assert alignment in from_raw_parts

This fixes the problem that we are fabricating pointers out of thin air. I also managed to share more code between the mutable and shared iterators, while reducing the amount of macros.

I am not sure how useful it really is to add a `debug_assert!` in libcore. Everybody gets a release version of that anyway, right? Is there at least a CI job that runs the test suite with a debug version?

Fixes #42789
  • Loading branch information
bors committed Jul 18, 2018
2 parents 12ed235 + 924225a commit 6ed2067
Show file tree
Hide file tree
Showing 3 changed files with 279 additions and 211 deletions.
46 changes: 46 additions & 0 deletions src/libcore/benches/slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,49 @@ fn binary_search_l2_with_dups(b: &mut Bencher) {
fn binary_search_l3_with_dups(b: &mut Bencher) {
binary_search(b, Cache::L3, |i| i / 16 * 16);
}

fn iter<T>(b: &mut Bencher, mapper: impl Fn(usize) -> T) {
let size = 1024;
let mut v = (0..size).map(mapper).collect::<Vec<T>>();
b.iter(move || {
let mut v = &mut v[..];
black_box(&mut v);
for i in v.iter() {
black_box(i);
}
});
}

fn iter_nth<T>(b: &mut Bencher, mapper: impl Fn(usize) -> T) {
let size = 1024;
let mut v = (0..size).map(mapper).collect::<Vec<T>>();
b.iter(move || {
let mut v = &mut v[..];
black_box(&mut v);
let mut it = v.iter();
for i in 0..size {
black_box(it.nth(0).unwrap());
black_box(v.iter().nth(i).unwrap());
}
});
}

#[bench]
fn iter_usize(b: &mut Bencher) {
iter(b, |i| i);
}

#[bench]
fn iter_zst(b: &mut Bencher) {
iter(b, |_i| ());
}

#[bench]
fn iter_nth_usize(b: &mut Bencher) {
iter_nth(b, |i| i);
}

#[bench]
fn iter_nth_zst(b: &mut Bencher) {
iter_nth(b, |_i| ());
}
Loading

0 comments on commit 6ed2067

Please sign in to comment.