diff options
author | sharkdp <davidpeter@web.de> | 2018-11-04 19:08:26 +0100 |
---|---|---|
committer | David Peter <sharkdp@users.noreply.github.com> | 2018-11-04 21:33:14 +0100 |
commit | b0cd527c62bd1b7139f685aa2d17293040fd7566 (patch) | |
tree | 7a19e184a31835bdbcc7aee64e1d1038d717508b /src | |
parent | 9c4c63f7571b61fdf77d878b434d4cf69e5095e7 (diff) |
Reduce number of syscalls
Diffstat (limited to 'src')
-rw-r--r-- | src/main.rs | 1 | ||||
-rw-r--r-- | src/walk.rs | 31 |
2 files changed, 17 insertions, 15 deletions
diff --git a/src/main.rs b/src/main.rs index 20b1333..966c3b3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,6 +3,7 @@ extern crate clap; extern crate humansize; extern crate num_cpus; extern crate rayon; +extern crate crossbeam_channel; mod walk; diff --git a/src/walk.rs b/src/walk.rs index cbf0656..b80df07 100644 --- a/src/walk.rs +++ b/src/walk.rs @@ -2,17 +2,18 @@ use std::collections::HashSet; use std::fs; use std::os::unix::fs::MetadataExt; use std::path::PathBuf; -use std::sync::mpsc::{channel, Sender}; use std::thread; +use crossbeam_channel as channel; + use rayon::prelude::*; type UniqueID = (u64, u64); type SizeEntry = (Option<UniqueID>, u64); -fn walk(tx: Sender<SizeEntry>, entries: &[PathBuf]) { - entries.par_iter().for_each_with(tx, |tx_ref, entry| { +fn walk(tx: channel::Sender<SizeEntry>, entries: &[PathBuf]) { + entries.into_par_iter().for_each_with(tx, |tx_ref, entry| { if let Ok(metadata) = entry.metadata() { // If the entry has more than one hard link, generate // a unique ID consisting of device and inode in order @@ -26,20 +27,20 @@ fn walk(tx: Sender<SizeEntry>, entries: &[PathBuf]) { let size = metadata.len(); tx_ref.send((unique_id, size)).unwrap(); - } else { - eprintln!("Could not get metadata: '{}'", entry.to_string_lossy()); - }; - if entry.is_dir() { - let mut children = vec![]; - for child_entry in fs::read_dir(entry).unwrap() { - if let Ok(child_entry) = child_entry { - let path = child_entry.path(); - children.push(PathBuf::from(path)); + if metadata.is_dir() { + let mut children = vec![]; + for child_entry in fs::read_dir(entry).unwrap() { + if let Ok(child_entry) = child_entry { + let path = child_entry.path(); + children.push(PathBuf::from(path)); + } } - } - walk(tx_ref.clone(), &children[..]); + walk(tx_ref.clone(), &children[..]); + }; + } else { + eprintln!("Could not get metadata: '{}'", entry.to_string_lossy()); }; }); } @@ -58,7 +59,7 @@ impl<'a> Walk<'a> { } pub fn run(&self) -> u64 { - let (tx, rx) = channel(); + let (tx, rx) = channel::unbounded(); let receiver_thread = thread::spawn(move || { let mut total = 0; |