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