Skip to content

Commit

Permalink
shootout-threadring rewrite
Browse files Browse the repository at this point in the history
* simplify the code
* remove trace to satisfy official shootout test
* use libgreen to improve performances
  • Loading branch information
TeXitoi committed Apr 21, 2014
1 parent a27dc53 commit 7265567
Showing 1 changed file with 19 additions and 49 deletions.
68 changes: 19 additions & 49 deletions src/test/bench/shootout-threadring.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
Expand All @@ -8,69 +8,39 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

// Based on threadring.erlang by Jira Isa

use std::os;
#![feature(phase)]
#[phase(syntax)] extern crate green;
green_start!(main)

fn start(n_tasks: int, token: int) {
let (tx, mut rx) = channel();
tx.send(token);
// FIXME could not get this to work with a range closure
let mut i = 2;
while i <= n_tasks {
for i in range(2, n_tasks + 1) {
let (tx, next_rx) = channel();
let imm_i = i;
let imm_rx = rx;
spawn(proc() {
roundtrip(imm_i, n_tasks, &imm_rx, &tx);
});
spawn(proc() roundtrip(i, tx, rx));
rx = next_rx;
i += 1;
}
let imm_rx = rx;
spawn(proc() {
roundtrip(1, n_tasks, &imm_rx, &tx);
});
spawn(proc() roundtrip(1, tx, rx));
}

fn roundtrip(id: int, n_tasks: int, p: &Receiver<int>, ch: &Sender<int>) {
loop {
match p.recv() {
1 => {
println!("{}\n", id);
return;
}
token => {
println!("thread: {} got token: {}", id, token);
ch.send(token - 1);
if token <= n_tasks {
return;
}
}
fn roundtrip(id: int, tx: Sender<int>, rx: Receiver<int>) {
for token in rx.iter() {
if token == 1 {
println!("{}", id);
break;
}
tx.send(token - 1);
}
}

fn main() {
use std::from_str::FromStr;

let args = if os::getenv("RUST_BENCH").is_some() {
vec!("".to_owned(), "2000000".to_owned(), "503".to_owned())
let args = std::os::args();
let token = if std::os::getenv("RUST_BENCH").is_some() {
2000000
} else {
os::args().move_iter().collect()
};
let token = if args.len() > 1u {
FromStr::from_str(*args.get(1)).unwrap()
}
else {
1000
args.get(1).and_then(|arg| from_str(*arg)).unwrap_or(1000)
};
let n_tasks = if args.len() > 2u {
FromStr::from_str(*args.get(2)).unwrap()
}
else {
503
};
start(n_tasks, token);
let n_tasks = args.get(2).and_then(|arg| from_str(*arg)).unwrap_or(503);

start(n_tasks, token);
}

5 comments on commit 7265567

@bors
Copy link
Contributor

@bors bors commented on 7265567 Apr 21, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

saw approval from alexcrichton
at TeXitoi@7265567

@bors
Copy link
Contributor

@bors bors commented on 7265567 Apr 21, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging TeXitoi/rust/shootout-threadring-rewrite = 7265567 into auto

@bors
Copy link
Contributor

@bors bors commented on 7265567 Apr 21, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TeXitoi/rust/shootout-threadring-rewrite = 7265567 merged ok, testing candidate = 829c00c

@bors
Copy link
Contributor

@bors bors commented on 7265567 Apr 21, 2014

@bors
Copy link
Contributor

@bors bors commented on 7265567 Apr 21, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fast-forwarding master to auto = 829c00c

Please sign in to comment.