From 7e3a4079e09d3ecbadaecea25c956e4c568ea996 Mon Sep 17 00:00:00 2001 From: trivernis Date: Mon, 16 Mar 2020 12:01:30 +0100 Subject: [PATCH] Increase number of read threads for decryption --- src/main.rs | 40 +++++++++++++++++++++++++--------------- 1 file changed, 25 insertions(+), 15 deletions(-) diff --git a/src/main.rs b/src/main.rs index 350e7e1..03b933e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -16,7 +16,6 @@ use spinners::{Spinner, Spinners}; use std::collections::HashMap; use std::fs; use std::fs::File; -use std::sync::mpsc::sync_channel; use std::sync::{Arc, Mutex}; use std::thread; use std::time::Duration; @@ -237,26 +236,37 @@ fn decrypt_with_dictionary_file(filename: String, data: &Vec) -> Option>(100); - let _handle = thread::spawn(move || { - let mut lookup_table = HashLookupTable::new(HashMap::new()); - if let Ok(table) = bdf_file.read_lookup_table() { - lookup_table = table.clone(); - } - while let Ok(next_chunk) = &mut bdf_file.next_chunk() { - if let Ok(entries) = next_chunk.data_entries(&lookup_table) { - if let Err(_) = rx.send(entries) {} + let bdf_arc = Arc::new(Mutex::new(bdf_file)); + let mut threads = Vec::new(); + let (rx, tx) = bounded::>(100); + + for _ in 0..(num_cpus::get() as f32 / 4f32).ceil() as usize { + let rx = rx.clone(); + let bdf_arc = Arc::clone(&bdf_arc); + + threads.push(thread::spawn(move || { + let mut lookup_table = HashLookupTable::new(HashMap::new()); + if let Some(table) = &bdf_arc.lock().unwrap().lookup_table { + lookup_table = table.clone(); } - } - }); + while let Ok(next_chunk) = &mut bdf_arc + .lock() + .expect("failed to lock bdf_arc to read next chunk") + .next_chunk() + { + if let Ok(entries) = next_chunk.data_entries(&lookup_table) { + if let Err(_) = rx.send(entries) {} + } + } + })); + } + drop(rx); sp.stop(); let mut result_data: Option> = None; for entries in tx {