Skip to content

Commit

Permalink
Rollup merge of rust-lang#104097 - RalfJung:miri-alloc-benches, r=thomcc
Browse files Browse the repository at this point in the history
run alloc benchmarks in Miri and fix UB

Miri since recently has a "fake monotonic clock" that works even with isolation. Its measurements are not very meaningful but it means we can run these benches and check them for UB.

And that's a good thing since there was UB here: fixes rust-lang#104096.

r? ``@thomcc``
  • Loading branch information
GuillaumeGomez authored Nov 8, 2022
2 parents afaba19 + 29d451c commit 53b6a89
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 16 deletions.
1 change: 0 additions & 1 deletion library/alloc/src/alloc/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ fn allocate_zeroed() {
}

#[bench]
#[cfg_attr(miri, ignore)] // isolated Miri does not support benchmarks
fn alloc_owned_small(b: &mut Bencher) {
b.iter(|| {
let _: Box<_> = Box::new(10);
Expand Down
34 changes: 19 additions & 15 deletions library/alloc/src/collections/vec_deque/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use core::iter::TrustedLen;
use super::*;

#[bench]
#[cfg_attr(miri, ignore)] // isolated Miri does not support benchmarks
fn bench_push_back_100(b: &mut test::Bencher) {
let mut deq = VecDeque::with_capacity(101);
b.iter(|| {
Expand All @@ -16,7 +15,6 @@ fn bench_push_back_100(b: &mut test::Bencher) {
}

#[bench]
#[cfg_attr(miri, ignore)] // isolated Miri does not support benchmarks
fn bench_push_front_100(b: &mut test::Bencher) {
let mut deq = VecDeque::with_capacity(101);
b.iter(|| {
Expand All @@ -29,12 +27,15 @@ fn bench_push_front_100(b: &mut test::Bencher) {
}

#[bench]
#[cfg_attr(miri, ignore)] // isolated Miri does not support benchmarks
fn bench_pop_back_100(b: &mut test::Bencher) {
let mut deq = VecDeque::<i32>::with_capacity(101);
let size = 100;
let mut deq = VecDeque::<i32>::with_capacity(size + 1);
// We'll mess with private state to pretend like `deq` is filled.
// Make sure the buffer is initialized so that we don't read uninit memory.
unsafe { deq.ptr().write_bytes(0u8, size + 1) };

b.iter(|| {
deq.head = 100;
deq.head = size;
deq.tail = 0;
while !deq.is_empty() {
test::black_box(deq.pop_back());
Expand All @@ -43,9 +44,9 @@ fn bench_pop_back_100(b: &mut test::Bencher) {
}

#[bench]
#[cfg_attr(miri, ignore)] // isolated Miri does not support benchmarks
fn bench_retain_whole_10000(b: &mut test::Bencher) {
let v = (1..100000).collect::<VecDeque<u32>>();
let size = if cfg!(miri) { 1000 } else { 100000 };
let v = (1..size).collect::<VecDeque<u32>>();

b.iter(|| {
let mut v = v.clone();
Expand All @@ -54,9 +55,9 @@ fn bench_retain_whole_10000(b: &mut test::Bencher) {
}

#[bench]
#[cfg_attr(miri, ignore)] // isolated Miri does not support benchmarks
fn bench_retain_odd_10000(b: &mut test::Bencher) {
let v = (1..100000).collect::<VecDeque<u32>>();
let size = if cfg!(miri) { 1000 } else { 100000 };
let v = (1..size).collect::<VecDeque<u32>>();

b.iter(|| {
let mut v = v.clone();
Expand All @@ -65,23 +66,26 @@ fn bench_retain_odd_10000(b: &mut test::Bencher) {
}

#[bench]
#[cfg_attr(miri, ignore)] // isolated Miri does not support benchmarks
fn bench_retain_half_10000(b: &mut test::Bencher) {
let v = (1..100000).collect::<VecDeque<u32>>();
let size = if cfg!(miri) { 1000 } else { 100000 };
let v = (1..size).collect::<VecDeque<u32>>();

b.iter(|| {
let mut v = v.clone();
v.retain(|x| *x > 50000)
v.retain(|x| *x > size / 2)
})
}

#[bench]
#[cfg_attr(miri, ignore)] // isolated Miri does not support benchmarks
fn bench_pop_front_100(b: &mut test::Bencher) {
let mut deq = VecDeque::<i32>::with_capacity(101);
let size = 100;
let mut deq = VecDeque::<i32>::with_capacity(size + 1);
// We'll mess with private state to pretend like `deq` is filled.
// Make sure the buffer is initialized so that we don't read uninit memory.
unsafe { deq.ptr().write_bytes(0u8, size + 1) };

b.iter(|| {
deq.head = 100;
deq.head = size;
deq.tail = 0;
while !deq.is_empty() {
test::black_box(deq.pop_front());
Expand Down

0 comments on commit 53b6a89

Please sign in to comment.