Skip to content

Commit c7cf7fe

Browse files
committed
Auto merge of #27636 - llogiq:patch-1, r=alexcrichton
See line 181: The lookup should start with the random index and iterate from there. Also locked stdout (which makes it a bit faster on my machine). And the `make_lookup` function now uses `map` (as the TODO asked for). Perhaps the multi-thread output from the fasta benchmark could be used to speed it up even more.
2 parents 2b45a0d + 86191e2 commit c7cf7fe

File tree

1 file changed

+13
-16
lines changed

1 file changed

+13
-16
lines changed

src/test/bench/shootout-fasta-redux.rs

+13-16
Original file line numberDiff line numberDiff line change
@@ -87,16 +87,12 @@ static HOMO_SAPIENS: [AminoAcid;4] = [
8787
AminoAcid { c: 't' as u8, p: 0.3015094502008 },
8888
];
8989

90-
// FIXME: Use map().
9190
fn sum_and_scale(a: &'static [AminoAcid]) -> Vec<AminoAcid> {
92-
let mut result = Vec::new();
9391
let mut p = 0f32;
94-
for a_i in a {
95-
let mut a_i = *a_i;
96-
p += a_i.p;
97-
a_i.p = p * LOOKUP_SCALE;
98-
result.push(a_i);
99-
}
92+
let mut result: Vec<AminoAcid> = a.iter().map(|a_i| {
93+
p += a_i.p;
94+
AminoAcid { c: a_i.c, p: p * LOOKUP_SCALE }
95+
}).collect();
10096
let result_len = result.len();
10197
result[result_len - 1].p = LOOKUP_SCALE;
10298
result
@@ -177,17 +173,17 @@ impl<'a, W: Write> RandomFasta<'a, W> {
177173

178174
fn rng(&mut self, max: f32) -> f32 {
179175
self.seed = (self.seed * IA + IC) % IM;
180-
max * (self.seed as f32) / (IM as f32)
176+
(max * self.seed as f32) / (IM as f32)
181177
}
182178

183179
fn nextc(&mut self) -> u8 {
184-
let r = self.rng(1.0);
185-
for a in &self.lookup[..] {
186-
if a.p >= r {
187-
return a.c;
180+
let r = self.rng(LOOKUP_SCALE);
181+
for i in (r as usize..LOOKUP_SIZE) {
182+
if self.lookup[i].p >= r {
183+
return self.lookup[i].c;
188184
}
189185
}
190-
0
186+
unreachable!();
191187
}
192188

193189
fn make(&mut self, n: usize) -> io::Result<()> {
@@ -216,8 +212,9 @@ fn main() {
216212
} else {
217213
5
218214
};
219-
220-
let mut out = io::stdout();
215+
216+
let stdout = io::stdout();
217+
let mut out = stdout.lock();
221218

222219
out.write_all(b">ONE Homo sapiens alu\n").unwrap();
223220
{

0 commit comments

Comments
 (0)