Skip to content

Commit 829c00c

Browse files
committed
auto merge of #13656 : TeXitoi/rust/shootout-threadring-rewrite, r=alexcrichton
* simplify the code * remove trace to satisfy official shootout test * use libgreen to improve performances
2 parents 0b77a49 + 7265567 commit 829c00c

File tree

1 file changed

+19
-49
lines changed

1 file changed

+19
-49
lines changed

src/test/bench/shootout-threadring.rs

+19-49
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
1+
// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
22
// file at the top-level directory of this distribution and at
33
// http://rust-lang.org/COPYRIGHT.
44
//
@@ -8,69 +8,39 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
// Based on threadring.erlang by Jira Isa
12-
13-
use std::os;
11+
#![feature(phase)]
12+
#[phase(syntax)] extern crate green;
13+
green_start!(main)
1414

1515
fn start(n_tasks: int, token: int) {
1616
let (tx, mut rx) = channel();
1717
tx.send(token);
18-
// FIXME could not get this to work with a range closure
19-
let mut i = 2;
20-
while i <= n_tasks {
18+
for i in range(2, n_tasks + 1) {
2119
let (tx, next_rx) = channel();
22-
let imm_i = i;
23-
let imm_rx = rx;
24-
spawn(proc() {
25-
roundtrip(imm_i, n_tasks, &imm_rx, &tx);
26-
});
20+
spawn(proc() roundtrip(i, tx, rx));
2721
rx = next_rx;
28-
i += 1;
2922
}
30-
let imm_rx = rx;
31-
spawn(proc() {
32-
roundtrip(1, n_tasks, &imm_rx, &tx);
33-
});
23+
spawn(proc() roundtrip(1, tx, rx));
3424
}
3525

36-
fn roundtrip(id: int, n_tasks: int, p: &Receiver<int>, ch: &Sender<int>) {
37-
loop {
38-
match p.recv() {
39-
1 => {
40-
println!("{}\n", id);
41-
return;
42-
}
43-
token => {
44-
println!("thread: {} got token: {}", id, token);
45-
ch.send(token - 1);
46-
if token <= n_tasks {
47-
return;
48-
}
49-
}
26+
fn roundtrip(id: int, tx: Sender<int>, rx: Receiver<int>) {
27+
for token in rx.iter() {
28+
if token == 1 {
29+
println!("{}", id);
30+
break;
5031
}
32+
tx.send(token - 1);
5133
}
5234
}
5335

5436
fn main() {
55-
use std::from_str::FromStr;
56-
57-
let args = if os::getenv("RUST_BENCH").is_some() {
58-
vec!("".to_owned(), "2000000".to_owned(), "503".to_owned())
37+
let args = std::os::args();
38+
let token = if std::os::getenv("RUST_BENCH").is_some() {
39+
2000000
5940
} else {
60-
os::args().move_iter().collect()
61-
};
62-
let token = if args.len() > 1u {
63-
FromStr::from_str(*args.get(1)).unwrap()
64-
}
65-
else {
66-
1000
41+
args.get(1).and_then(|arg| from_str(*arg)).unwrap_or(1000)
6742
};
68-
let n_tasks = if args.len() > 2u {
69-
FromStr::from_str(*args.get(2)).unwrap()
70-
}
71-
else {
72-
503
73-
};
74-
start(n_tasks, token);
43+
let n_tasks = args.get(2).and_then(|arg| from_str(*arg)).unwrap_or(503);
7544

45+
start(n_tasks, token);
7646
}

0 commit comments

Comments
 (0)