rust/src/test/bench/task-perf-alloc-unwind.rs

95 lines
2.1 KiB
Rust
Raw Normal View History

// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
#![feature(box_syntax, duration, duration_span, vec_push_all)]
2013-10-23 04:49:18 -04:00
use std::env;
2015-02-17 15:10:25 -08:00
use std::thread;
use std::time::Duration;
2012-06-02 17:42:48 -07:00
#[derive(Clone)]
enum List<T> {
2014-10-02 08:10:09 +03:00
Nil, Cons(T, Box<List<T>>)
}
2012-06-02 17:42:48 -07:00
fn main() {
let (repeat, depth) = if env::var_os("RUST_BENCH").is_some() {
2012-06-02 17:42:48 -07:00
(50, 1000)
} else {
(10, 10)
};
run(repeat, depth);
}
fn run(repeat: isize, depth: isize) {
for _ in 0..repeat {
let dur = Duration::span(|| {
2015-02-17 15:10:25 -08:00
let _ = thread::spawn(move|| {
recurse_or_panic(depth, None)
}).join();
2014-01-27 18:29:50 -05:00
});
println!("iter: {:?}", dur);
2012-06-02 17:42:48 -07:00
}
}
type nillist = List<()>;
2012-06-02 17:42:48 -07:00
// Filled with things that have to be unwound
2013-01-28 18:55:44 -08:00
struct State {
unique: Box<nillist>,
2014-10-02 08:10:09 +03:00
vec: Vec<Box<nillist>>,
2013-01-28 18:55:44 -08:00
res: r
2012-06-02 17:42:48 -07:00
}
2012-08-15 18:46:55 -07:00
struct r {
2014-10-02 08:10:09 +03:00
_l: Box<nillist>,
}
impl Drop for r {
2013-09-16 21:18:07 -04:00
fn drop(&mut self) {}
2012-06-02 17:42:48 -07:00
}
2014-10-02 08:10:09 +03:00
fn r(l: Box<nillist>) -> r {
2012-09-05 15:58:43 -07:00
r {
_l: l
}
}
fn recurse_or_panic(depth: isize, st: Option<State>) {
2012-06-02 17:42:48 -07:00
if depth == 0 {
panic!();
2012-06-02 17:42:48 -07:00
} else {
let depth = depth - 1;
2012-08-06 12:34:08 -07:00
let st = match st {
None => {
State {
unique: box List::Nil,
vec: vec!(box List::Nil),
res: r(box List::Nil)
}
2013-01-28 18:55:44 -08:00
}
Some(st) => {
let mut v = st.vec.clone();
v.push_all(&[box List::Cons((), st.vec.last().unwrap().clone())]);
State {
unique: box List::Cons((), box *st.unique),
vec: v,
res: r(box List::Cons((), st.res._l.clone())),
}
2013-01-28 18:55:44 -08:00
}
2012-06-02 17:42:48 -07:00
};
recurse_or_panic(depth, Some(st));
2012-06-02 17:42:48 -07:00
}
}