]> git.lizzy.rs Git - rust.git/blob - src/test/bench/task-perf-alloc-unwind.rs
Merge pull request #20510 from tshepang/patch-6
[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(unsafe_destructor)]
12
13 use std::os;
14 use std::thread::Thread;
15 use std::time::Duration;
16
17 #[derive(Clone)]
18 enum List<T> {
19     Nil, Cons(T, Box<List<T>>)
20 }
21
22 enum UniqueList {
23     ULNil, ULCons(Box<UniqueList>)
24 }
25
26 fn main() {
27     let (repeat, depth) = if os::getenv("RUST_BENCH").is_some() {
28         (50, 1000)
29     } else {
30         (10, 10)
31     };
32
33     run(repeat, depth);
34 }
35
36 fn run(repeat: int, depth: int) {
37     for _ in range(0, repeat) {
38         let dur = Duration::span(|| {
39             let _ = Thread::spawn(move|| {
40                 recurse_or_panic(depth, None)
41             }).join();
42         });
43         println!("iter: {}", dur);
44     }
45 }
46
47 type nillist = List<()>;
48
49 // Filled with things that have to be unwound
50
51 struct State {
52     unique: Box<nillist>,
53     vec: Vec<Box<nillist>>,
54     res: r
55 }
56
57 struct r {
58   _l: Box<nillist>,
59 }
60
61 #[unsafe_destructor]
62 impl Drop for r {
63     fn drop(&mut self) {}
64 }
65
66 fn r(l: Box<nillist>) -> r {
67     r {
68         _l: l
69     }
70 }
71
72 fn recurse_or_panic(depth: int, st: Option<State>) {
73     if depth == 0 {
74         panic!();
75     } else {
76         let depth = depth - 1;
77
78         let st = match st {
79             None => {
80                 State {
81                     unique: box List::Nil,
82                     vec: vec!(box List::Nil),
83                     res: r(box List::Nil)
84                 }
85             }
86             Some(st) => {
87                 let mut v = st.vec.clone();
88                 v.push_all(&[box List::Cons((), st.vec.last().unwrap().clone())]);
89                 State {
90                     unique: box List::Cons((), box *st.unique),
91                     vec: v,
92                     res: r(box List::Cons((), st.res._l.clone())),
93                 }
94             }
95         };
96
97         recurse_or_panic(depth, Some(st));
98     }
99 }