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