How can we substantially speed up this function?: /// Store the isoforms in a HashMap in…
- User
How can we substantially speed up this function?:
/// Store the isoforms in a HashMap in the form: isoform -> gene. /// Provides a fast access to the gene name given an isoform name. fn get_isoforms(path: PathBuf) -> Result<HashMap<String, String>, Box> { let file: File = File::open(path).unwrap(); let reader: BufReader = BufReader::new(file); let mut isoforms: HashMap<String, String> = HashMap::new();
for line in reader.lines() { let line = line.unwrap(); let content: Vec<&str> = line.split("\t").collect(); let gene: String = content[0].to_string(); let isoform: String = content[1].to_string(); isoforms.insert(isoform, gene); } return Ok(isoforms);}
I initially came with this solution:
/// Store the isoforms in a HashMap in the form: isoform -> gene. /// Provides a fast access to the gene name given an isoform name. fn get_isoforms(path: PathBuf) -> Result<HashMap<String, String>, Box> { let file: File = File::open(path).unwrap(); let reader: BufReader = BufReader::new(file); let mut isoforms: HashMap<String, String> = HashMap::new();
for line in reader.lines() { let line = line.unwrap(); let bytes = line.as_bytes(); let sp = memchr::memchr(b'\t', &bytes).unwrap(); let content: [&str; 2] = [ unsafe { std::str::from_utf8_unchecked(&bytes[0..sp]) }, unsafe { std::str::from_utf8_unchecked(&bytes[sp + 1..]) }, ]; let gene: String = content[0].to_string(); let isoform: String = content[1].to_string(); isoforms.insert(isoform, gene); } return Ok(isoforms);}
but does not seem to help that much
- Assistant
- User
is it there another way to even enhance performance more? I tested the three functions and the function you provided takes around 85ms whereas the unsafe one takes ~80ms and the first version takes 100-90 ms. I am thinking maybe read in parallel or SIMD?
- Assistant
- User
and what do you think about this one:
fn bedsort(bed: &String) -> Result<Vec<(String, i32, String)>, ParseError> { let bedfile = File::open(PathBuf::from(bed)).unwrap(); let reader = BufReader::new(bedfile); let mut tmp: Vec<(String, i32, String)> = Vec::new();
for line in reader.lines() { let record = BedRecord::layer(&line?)?; tmp.push(record); } tmp.sort_by(|a, b| { let cmp_chr = compare(&a.0, &b.0); if cmp_chr == std::cmp::Ordering::Equal { a.1.cmp(&b.1) } else { cmp_chr } }); Ok(tmp)}
how could you significantly increase its speed and performance?
- Assistant