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