Permalink
Browse files

Add improvements suggested by erickt and bblum

  • Loading branch information...
1 parent 2bd482e commit 638ee23fcf656b8af6612b75ea0401318a7e6470 @killerswan killerswan committed with brson Jul 8, 2012
Showing with 16 additions and 29 deletions.
  1. +16 −29 src/test/bench/shootout-chameneos-redux.rs
@@ -17,9 +17,7 @@ fn print_complements() {
}
}
-// can I combine these two lines?
-enum color_e { Red, Yellow, Blue }
-type color = color_e;
+enum color { Red, Yellow, Blue }
type creature_info = { name: uint, color: color };
@@ -125,49 +123,38 @@ fn creature(
}
fn rendezvous(nn: uint, set: ~[color]) {
+ // these ports will allow us to hear from the creatures
let from_creatures: comm::port<creature_info> = comm::port();
let from_creatures_log: comm::port<str> = comm::port();
+
+ // these channels will be passed to the creatures so they can talk to us
let to_rendezvous = comm::chan(from_creatures);
let to_rendezvous_log = comm::chan(from_creatures_log);
+
+ // these channels will allow us to talk to each creature by 'name'/index
let to_creature: ~[comm::chan<option<creature_info>>] =
vec::mapi(set,
fn@(ii: uint, col: color) -> comm::chan<option<creature_info>> {
+ // create each creature as a listener with a port, and
+ // give us a channel to talk to each
ret do task::spawn_listener |from_rendezvous| {
creature(ii, col, from_rendezvous, to_rendezvous,
to_rendezvous_log);
};
}
);
- let mut meetings = 0;
let mut creatures_met = 0;
- let mut creatures_present = 0;
-
- // use option type instead of initializing to junk?
- let mut first_creature = { name: 0, color: Red };
- let mut second_creature = { name: 0, color: Red };
// set up meetings...
- while meetings < nn {
- let creature_req: creature_info = comm::recv(from_creatures);
- creatures_met += 1;
-
- alt creatures_present {
- 0 {
- first_creature = creature_req;
- creatures_present = 1;
- }
- 1 {
- second_creature = creature_req;
- comm::send(to_creature[first_creature.name],
- some(second_creature));
- comm::send(to_creature[second_creature.name],
- some(first_creature));
- creatures_present = 0;
- meetings += 1;
- }
- _ { fail "too many creatures are here!" }
- }
+ for nn.times {
+ let fst_creature: creature_info = comm::recv(from_creatures);
+ let snd_creature: creature_info = comm::recv(from_creatures);
+
+ creatures_met += 2;
+
+ comm::send(to_creature[fst_creature.name], some(snd_creature));
+ comm::send(to_creature[snd_creature.name], some(fst_creature));
}
// tell each creature to stop

0 comments on commit 638ee23

Please sign in to comment.