]> git.lizzy.rs Git - rust.git/blob - src/test/bench/task-perf-alloc-unwind.rs
auto merge of #13967 : richo/rust/features/ICE-fails, r=alexcrichton
[rust.git] / src / test / bench / task-perf-alloc-unwind.rs
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
11 #![feature(managed_boxes)]
12
13 extern crate collections;
14 extern crate time;
15
16 use time::precise_time_s;
17 use std::os;
18 use std::task;
19 use std::vec;
20
21 #[deriving(Clone)]
22 enum List<T> {
23     Nil, Cons(T, @List<T>)
24 }
25
26 enum UniqueList {
27     ULNil, ULCons(Box<UniqueList>)
28 }
29
30 fn main() {
31     let (repeat, depth) = if os::getenv("RUST_BENCH").is_some() {
32         (50, 1000)
33     } else {
34         (10, 10)
35     };
36
37     run(repeat, depth);
38 }
39
40 fn run(repeat: int, depth: int) {
41     for _ in range(0, repeat) {
42         println!("starting {:.4f}", precise_time_s());
43         task::try(proc() {
44             recurse_or_fail(depth, None)
45         });
46         println!("stopping {:.4f}", precise_time_s());
47     }
48 }
49
50 type nillist = List<()>;
51
52 // Filled with things that have to be unwound
53
54 struct State {
55     managed: @nillist,
56     unique: Box<nillist>,
57     tuple: (@nillist, Box<nillist>),
58     vec: Vec<@nillist>,
59     res: r
60 }
61
62 struct r {
63   _l: @nillist,
64 }
65
66 #[unsafe_destructor]
67 impl Drop for r {
68     fn drop(&mut self) {}
69 }
70
71 fn r(l: @nillist) -> r {
72     r {
73         _l: l
74     }
75 }
76
77 fn recurse_or_fail(depth: int, st: Option<State>) {
78     if depth == 0 {
79         println!("unwinding {:.4f}", precise_time_s());
80         fail!();
81     } else {
82         let depth = depth - 1;
83
84         let st = match st {
85           None => {
86             State {
87                 managed: @Nil,
88                 unique: box Nil,
89                 tuple: (@Nil, box Nil),
90                 vec: vec!(@Nil),
91                 res: r(@Nil)
92             }
93           }
94           Some(st) => {
95             State {
96                 managed: @Cons((), st.managed),
97                 unique: box Cons((), @*st.unique),
98                 tuple: (@Cons((), st.tuple.ref0().clone()),
99                         box Cons((), @*st.tuple.ref1().clone())),
100                 vec: st.vec.clone().append(&[@Cons((), *st.vec.last().unwrap())]),
101                 res: r(@Cons((), st.res._l))
102             }
103           }
104         };
105
106         recurse_or_fail(depth, Some(st));
107     }
108 }