Skip to content

reverse_complement: Pre-allocate a buffer based on input size #44

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 24, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions src/reverse_complement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// http://benchmarksgame.alioth.debian.org/
//
// contributed by the Rust Project Developers
// contributed by Matt Brubeck
// contributed by TeXitoi

//extern crate libc;
Expand All @@ -20,6 +21,8 @@ mod libc {
use std::io::{Read, Write};
use std::ptr::copy;
use std::thread;
use std::fs::File;
use std::os::unix::io::FromRawFd;

struct Tables {
table8: [u8;1 << 8],
Expand Down Expand Up @@ -187,10 +190,15 @@ fn parallel<'a, I, T, F>(iter: I, ref f: F)
for jh in jhs { jh.join().unwrap(); }
}

fn file_size(f: &mut File) -> std::io::Result<usize> {
Ok(f.metadata()?.len() as usize)
}

fn main() {
let stdin = std::io::stdin();
let mut data = Vec::with_capacity(1024 * 1024);
stdin.lock().read_to_end(&mut data).unwrap();
let mut stdin = unsafe { File::from_raw_fd(0) };
let size = file_size(&mut stdin).unwrap_or(1024 * 1024);
let mut data = Vec::with_capacity(size + 1);
stdin.read_to_end(&mut data).unwrap();
let tables = &Tables::new();
parallel(mut_dna_seqs(&mut data), |seq| reverse_complement(seq, tables));
let stdout = std::io::stdout();
Expand Down