]> git.proxmox.com Git - rustc.git/blame - src/test/bench/task-perf-alloc-unwind.rs
Imported Upstream version 1.0.0~beta
[rustc.git] / src / test / bench / task-perf-alloc-unwind.rs
CommitLineData
223e47cc
LB
1// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
2// file at the top-level directory of this distribution and at
3// http://rust-lang.org/COPYRIGHT.
4//
5// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8// option. This file may not be copied, modified, or distributed
9// except according to those terms.
10
c34b1796 11#![feature(unsafe_destructor, box_syntax, std_misc, collections)]
223e47cc 12
85aaf69f
SL
13use std::env;
14use std::thread;
1a4d82fc
JJ
15use std::time::Duration;
16
17#[derive(Clone)]
18enum List<T> {
19 Nil, Cons(T, Box<List<T>>)
20}
223e47cc 21
223e47cc 22fn main() {
85aaf69f 23 let (repeat, depth) = if env::var_os("RUST_BENCH").is_some() {
223e47cc
LB
24 (50, 1000)
25 } else {
26 (10, 10)
27 };
28
29 run(repeat, depth);
30}
31
c34b1796 32fn run(repeat: isize, depth: isize) {
85aaf69f 33 for _ in 0..repeat {
1a4d82fc 34 let dur = Duration::span(|| {
85aaf69f 35 let _ = thread::spawn(move|| {
1a4d82fc
JJ
36 recurse_or_panic(depth, None)
37 }).join();
38 });
39 println!("iter: {}", dur);
223e47cc
LB
40 }
41}
42
c34b1796 43type nillist = List<()>;
223e47cc
LB
44
45// Filled with things that have to be unwound
46
47struct State {
1a4d82fc
JJ
48 unique: Box<nillist>,
49 vec: Vec<Box<nillist>>,
223e47cc
LB
50 res: r
51}
52
53struct r {
1a4d82fc 54 _l: Box<nillist>,
223e47cc
LB
55}
56
57#[unsafe_destructor]
58impl Drop for r {
1a4d82fc 59 fn drop(&mut self) {}
223e47cc
LB
60}
61
1a4d82fc 62fn r(l: Box<nillist>) -> r {
223e47cc
LB
63 r {
64 _l: l
65 }
66}
67
c34b1796 68fn recurse_or_panic(depth: isize, st: Option<State>) {
223e47cc 69 if depth == 0 {
1a4d82fc 70 panic!();
223e47cc
LB
71 } else {
72 let depth = depth - 1;
73
74 let st = match st {
1a4d82fc
JJ
75 None => {
76 State {
77 unique: box List::Nil,
78 vec: vec!(box List::Nil),
79 res: r(box List::Nil)
80 }
223e47cc 81 }
1a4d82fc
JJ
82 Some(st) => {
83 let mut v = st.vec.clone();
c34b1796 84 v.push_all(&[box List::Cons((), st.vec.last().unwrap().clone())]);
1a4d82fc 85 State {
c34b1796 86 unique: box List::Cons((), box *st.unique),
1a4d82fc 87 vec: v,
c34b1796 88 res: r(box List::Cons((), st.res._l.clone())),
1a4d82fc 89 }
223e47cc 90 }
223e47cc
LB
91 };
92
1a4d82fc 93 recurse_or_panic(depth, Some(st));
223e47cc
LB
94 }
95}