2012-12-10 17:32:48 -08:00
|
|
|
// 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.
|
|
|
|
|
2015-06-10 13:33:52 -07:00
|
|
|
#![feature(box_syntax, duration, duration_span, vec_push_all)]
|
2013-10-23 04:49:18 -04:00
|
|
|
|
2015-02-13 22:08:05 +02:00
|
|
|
use std::env;
|
2015-02-17 15:10:25 -08:00
|
|
|
use std::thread;
|
2014-11-10 12:27:56 -08:00
|
|
|
use std::time::Duration;
|
2012-06-02 17:42:48 -07:00
|
|
|
|
2014-12-31 17:32:49 +13:00
|
|
|
#[derive(Clone)]
|
2014-03-18 21:31:40 -07:00
|
|
|
enum List<T> {
|
2014-10-02 08:10:09 +03:00
|
|
|
Nil, Cons(T, Box<List<T>>)
|
2014-03-18 21:31:40 -07:00
|
|
|
}
|
|
|
|
|
2012-06-02 17:42:48 -07:00
|
|
|
fn main() {
|
2015-02-13 22:08:05 +02:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2015-03-25 17:06:52 -07:00
|
|
|
fn run(repeat: isize, depth: isize) {
|
2015-01-26 15:46:12 -05:00
|
|
|
for _ in 0..repeat {
|
2014-11-10 12:27:56 -08:00
|
|
|
let dur = Duration::span(|| {
|
2015-02-17 15:10:25 -08:00
|
|
|
let _ = thread::spawn(move|| {
|
2014-11-10 12:27:56 -08:00
|
|
|
recurse_or_panic(depth, None)
|
2015-01-01 23:53:35 -08:00
|
|
|
}).join();
|
2014-01-27 18:29:50 -05:00
|
|
|
});
|
2015-07-05 23:20:00 -07:00
|
|
|
println!("iter: {:?}", dur);
|
2012-06-02 17:42:48 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-26 15:22:13 +02: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 {
|
2014-05-05 18:56:44 -07:00
|
|
|
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>,
|
2012-11-14 01:22:37 -05:00
|
|
|
}
|
|
|
|
|
2013-02-14 11:47:00 -08:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-25 17:06:52 -07:00
|
|
|
fn recurse_or_panic(depth: isize, st: Option<State>) {
|
2012-06-02 17:42:48 -07:00
|
|
|
if depth == 0 {
|
2014-10-09 15:17:22 -04:00
|
|
|
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 {
|
2014-10-14 23:05:01 -07:00
|
|
|
None => {
|
|
|
|
State {
|
2014-11-06 00:05:53 -08:00
|
|
|
unique: box List::Nil,
|
|
|
|
vec: vec!(box List::Nil),
|
|
|
|
res: r(box List::Nil)
|
2014-10-14 23:05:01 -07:00
|
|
|
}
|
2013-01-28 18:55:44 -08:00
|
|
|
}
|
2014-10-14 23:05:01 -07:00
|
|
|
Some(st) => {
|
|
|
|
let mut v = st.vec.clone();
|
2015-02-26 15:22:13 +02:00
|
|
|
v.push_all(&[box List::Cons((), st.vec.last().unwrap().clone())]);
|
2014-10-14 23:05:01 -07:00
|
|
|
State {
|
2015-02-26 15:22:13 +02:00
|
|
|
unique: box List::Cons((), box *st.unique),
|
2014-10-14 23:05:01 -07:00
|
|
|
vec: v,
|
2015-02-26 15:22:13 +02:00
|
|
|
res: r(box List::Cons((), st.res._l.clone())),
|
2014-10-14 23:05:01 -07:00
|
|
|
}
|
2013-01-28 18:55:44 -08:00
|
|
|
}
|
2012-06-02 17:42:48 -07:00
|
|
|
};
|
|
|
|
|
2014-10-09 15:17:22 -04:00
|
|
|
recurse_or_panic(depth, Some(st));
|
2012-06-02 17:42:48 -07:00
|
|
|
}
|
|
|
|
}
|