]> git.lizzy.rs Git - rust.git/blob - src/test/bench/task-perf-alloc-unwind.rs
1a33391a3d2e98fdacd264124f951729052cb4fa
[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 collections::list::{List, Cons, Nil};
17 use time::precise_time_s;
18 use std::os;
19 use std::task;
20
21 enum UniqueList {
22     ULNil, ULCons(~UniqueList)
23 }
24
25 fn main() {
26     let (repeat, depth) = if os::getenv("RUST_BENCH").is_some() {
27         (50, 1000)
28     } else {
29         (10, 10)
30     };
31
32     run(repeat, depth);
33 }
34
35 fn run(repeat: int, depth: int) {
36     for _ in range(0, repeat) {
37         println!("starting {:.4f}", precise_time_s());
38         task::try(proc() {
39             recurse_or_fail(depth, None)
40         });
41         println!("stopping {:.4f}", precise_time_s());
42     }
43 }
44
45 type nillist = List<()>;
46
47 // Filled with things that have to be unwound
48
49 struct State {
50     managed: @nillist,
51     unique: ~nillist,
52     tuple: (@nillist, ~nillist),
53     vec: 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(&mut 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         println!("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                 managed: @Nil,
83                 unique: ~Nil,
84                 tuple: (@Nil, ~Nil),
85                 vec: vec!(@Nil),
86                 res: r(@Nil)
87             }
88           }
89           Some(st) => {
90             State {
91                 managed: @Cons((), st.managed),
92                 unique: ~Cons((), @*st.unique),
93                 tuple: (@Cons((), st.tuple.ref0().clone()),
94                         ~Cons((), @*st.tuple.ref1().clone())),
95                 vec: st.vec + &[@Cons((), *st.vec.last().unwrap())],
96                 res: r(@Cons((), st.res._l))
97             }
98           }
99         };
100
101         recurse_or_fail(depth, Some(st));
102     }
103 }