Skip to content

Commit 8e1fa8c

Browse files
committed
add more benchmarks
1 parent cecae80 commit 8e1fa8c

File tree

5 files changed

+121
-38
lines changed

5 files changed

+121
-38
lines changed

benches/fibonacci.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#![feature(custom_attribute, test)]
2+
#![feature(rustc_private)]
3+
#![allow(unused_attributes)]
4+
5+
extern crate test;
6+
use test::Bencher;
7+
8+
mod fibonacci_helper;
9+
10+
#[bench]
11+
fn fib(bencher: &mut Bencher) {
12+
bencher.iter(|| {
13+
fibonacci_helper::main();
14+
})
15+
}
16+
17+
mod miri_helper;
18+
19+
#[bench]
20+
fn fib_miri(bencher: &mut Bencher) {
21+
miri_helper::run("fibonacci_helper", bencher);
22+
}
23+
24+
mod fibonacci_helper_iterative;
25+
26+
#[bench]
27+
fn fib_iter(bencher: &mut Bencher) {
28+
bencher.iter(|| {
29+
fibonacci_helper_iterative::main();
30+
})
31+
}
32+
33+
#[bench]
34+
fn fib_iter_miri(bencher: &mut Bencher) {
35+
miri_helper::run("fibonacci_helper_iterative", bencher);
36+
}

benches/fibonacci_helper.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#![feature(custom_attribute)]
2+
#![allow(unused_attributes)]
3+
4+
#[miri_run]
5+
#[inline(never)]
6+
pub fn main() {
7+
assert_eq!(fib(10), 55);
8+
}
9+
10+
fn fib(n: usize) -> usize {
11+
if n <= 2 {
12+
1
13+
} else {
14+
fib(n - 1) + fib(n - 2)
15+
}
16+
}

benches/fibonacci_helper_iterative.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#![feature(custom_attribute)]
2+
#![allow(unused_attributes)]
3+
4+
#[miri_run]
5+
#[inline(never)]
6+
pub fn main() {
7+
assert_eq!(fib(10), 55);
8+
}
9+
10+
fn fib(n: usize) -> usize {
11+
let mut a = 0;
12+
let mut b = 1;
13+
for _ in 0..n {
14+
let c = a;
15+
a = b;
16+
b = c + b;
17+
}
18+
a
19+
}

benches/miri_helper.rs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#![feature(custom_attribute, test)]
2+
#![feature(rustc_private)]
3+
#![allow(unused_attributes)]
4+
5+
extern crate getopts;
6+
extern crate miri;
7+
extern crate rustc;
8+
extern crate rustc_driver;
9+
extern crate test;
10+
11+
use self::miri::interpreter;
12+
use self::rustc::session::Session;
13+
use self::rustc_driver::{driver, CompilerCalls};
14+
use std::cell::RefCell;
15+
use std::rc::Rc;
16+
use std::env::var;
17+
use test::Bencher;
18+
19+
pub struct MiriCompilerCalls<'a>(Rc<RefCell<&'a mut Bencher>>);
20+
21+
pub fn run(filename: &str, bencher: &mut Bencher) {
22+
let path = var("RUST_SYSROOT").expect("env variable `RUST_SYSROOT` not set");
23+
rustc_driver::run_compiler(&[
24+
"miri".to_string(), format!("benches/{}.rs", filename), "--sysroot".to_string(), path.to_string(),
25+
], &mut MiriCompilerCalls(Rc::new(RefCell::new(bencher))));
26+
}
27+
28+
impl<'a> CompilerCalls<'a> for MiriCompilerCalls<'a> {
29+
fn build_controller(
30+
&mut self,
31+
_: &Session,
32+
_: &getopts::Matches
33+
) -> driver::CompileController<'a> {
34+
let mut control: driver::CompileController<'a> = driver::CompileController::basic();
35+
36+
let bencher = self.0.clone();
37+
38+
control.after_analysis.callback = Box::new(move |state| {
39+
state.session.abort_if_errors();
40+
bencher.borrow_mut().iter(|| {
41+
interpreter::interpret_start_points(state.tcx.unwrap(), state.mir_map.unwrap());
42+
})
43+
});
44+
45+
control
46+
}
47+
}

benches/smoke.rs

Lines changed: 3 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,6 @@
22
#![feature(rustc_private)]
33
#![allow(unused_attributes)]
44

5-
extern crate getopts;
6-
extern crate miri;
7-
extern crate rustc;
8-
extern crate rustc_driver;
9-
10-
use miri::interpreter;
11-
use rustc::session::Session;
12-
use rustc_driver::{driver, CompilerCalls};
13-
use std::cell::RefCell;
14-
use std::rc::Rc;
15-
165
extern crate test;
176
use test::Bencher;
187

@@ -44,33 +33,9 @@ fn noop_miri_full(bencher: &mut Bencher) {
4433
}
4534
*/
4635

36+
mod miri_helper;
37+
4738
#[bench]
4839
fn noop_miri_interpreter(bencher: &mut Bencher) {
49-
let path = std::env::var("RUST_SYSROOT").expect("env variable `RUST_SYSROOT` not set");
50-
rustc_driver::run_compiler(&[
51-
"miri".to_string(), "benches/smoke_helper.rs".to_string(), "--sysroot".to_string(), path.to_string(),
52-
], &mut MiriCompilerCalls(Rc::new(RefCell::new(bencher))));
53-
}
54-
55-
struct MiriCompilerCalls<'a>(Rc<RefCell<&'a mut Bencher>>);
56-
57-
impl<'a> CompilerCalls<'a> for MiriCompilerCalls<'a> {
58-
fn build_controller(
59-
&mut self,
60-
_: &Session,
61-
_: &getopts::Matches
62-
) -> driver::CompileController<'a> {
63-
let mut control: driver::CompileController<'a> = driver::CompileController::basic();
64-
65-
let bencher = self.0.clone();
66-
67-
control.after_analysis.callback = Box::new(move |state| {
68-
state.session.abort_if_errors();
69-
bencher.borrow_mut().iter(|| {
70-
interpreter::interpret_start_points(state.tcx.unwrap(), state.mir_map.unwrap());
71-
})
72-
});
73-
74-
control
75-
}
40+
miri_helper::run("smoke_helper", bencher);
7641
}

0 commit comments

Comments
 (0)