sequential file read slightly faster

This commit is contained in:
JestDotty 2025-03-24 16:03:36 -04:00
parent f9115a9e40
commit 4415dd26ae

View File

@ -51,12 +51,7 @@ static FORBIDDEN_WORDS: LazyLock<Trie> = LazyLock::new(|| {
}); });
impl Stats { impl Stats {
pub fn process(&mut self, file: &str) { pub fn process(&mut self, text: &str) {
let Ok(text) = fs::read_to_string(&file) else {
self.failed_file_count += 1;
return;
};
self.file_count += 1;
// self.muncher(&text); // self.muncher(&text);
self.for_loops(&text); self.for_loops(&text);
} }
@ -166,20 +161,25 @@ impl Stats {
#[tokio::main] #[tokio::main]
async fn main() { async fn main() {
let files = env::args().skip(1); let files = env::args().skip(1);
let mut stats = Stats::default();
let mut rx = { let mut rx = {
let (tx, rx) = mpsc::unbounded_channel(); let (tx, rx) = mpsc::unbounded_channel();
for file in files { for file in files {
//reading files not sequentially average shaves 30ms (of 1250ms), and that's on a NVMe SSD so why not
let Ok(text) = fs::read_to_string(&file) else {
stats.failed_file_count += 1;
continue;
};
stats.file_count += 1;
let tx = tx.clone(); let tx = tx.clone();
tokio::spawn(async move { tokio::spawn(async move {
let mut stats = Stats::default(); let mut stats = Stats::default();
stats.process(&file); stats.process(&text);
tx.send(stats).unwrap(); tx.send(stats).unwrap();
}); });
} }
rx rx
}; };
let mut stats = Stats::default();
while let Some(file_stat) = rx.recv().await { while let Some(file_stat) = rx.recv().await {
stats += file_stat; stats += file_stat;
} }