From aa091b69c8faddc9515a47ad8e66d4c89a91ac05 Mon Sep 17 00:00:00 2001 From: rabite Date: Wed, 5 Feb 2020 18:55:09 +0100 Subject: [PATCH] more prallel processing --- Cargo.lock | 1 + Cargo.toml | 3 ++- src/files.rs | 44 ++++++++++++++++++++++++++++++++++++++------ 3 files changed, 41 insertions(+), 7 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index bbee8dd..e3167a0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -594,6 +594,7 @@ dependencies = [ "gstreamer-player 0.14.0 (registry+https://github.com/rust-lang/crates.io-index)", "gstreamer-video 0.14.5 (registry+https://github.com/rust-lang/crates.io-index)", "image 0.21.3 (registry+https://github.com/rust-lang/crates.io-index)", + "itertools 0.8.2 (registry+https://github.com/rust-lang/crates.io-index)", "lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.2.66 (registry+https://github.com/rust-lang/crates.io-index)", "lscolors 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", diff --git a/Cargo.toml b/Cargo.toml index 48080c6..f43b1d8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -20,7 +20,7 @@ lazy_static = "1" alphanumeric-sort = "1.0.6" lscolors = { version = "0.5.0", features = [ "ansi_term" ] } tree_magic = "0.2.1" -rayon = "1.0.3" +rayon = "1.3" dirs-2 = "1.1.0" users = "0.8" chrono = "0.4" @@ -42,6 +42,7 @@ strum = "0.15" strum_macros = "0.15" rust-ini = "0.13" derivative = "1.0.3" +itertools = "0.8" image = { version = "0.21.1", optional = true } diff --git a/src/files.rs b/src/files.rs index fbeaba0..c7d250a 100644 --- a/src/files.rs +++ b/src/files.rs @@ -378,14 +378,46 @@ impl Files { } pub fn recalculate_len(&mut self) { - self.len = self.iter_files().count(); + self.len = self.par_iter_files().count(); } pub fn get_file_mut(&mut self, index: usize) -> Option<&mut File> { - self.iter_files_mut() - .nth(index) + self.par_iter_files_mut() + .find_first(|(i, _)| *i == index) + .map(|(_, f)| f) } + pub fn par_iter_files(&self) -> impl ParallelIterator { + let filter = self.filter.clone(); + let filter_selected = self.filter_selected; + let show_hidden = self.show_hidden; + + self.files + .par_iter() + .filter(move |f| + f.kind == Kind::Placeholder || + !(filter.is_some() && + !f.name.contains(filter.as_ref().unwrap())) && + (!filter_selected || f.selected)) + .filter(move |f| !(!show_hidden && f.hidden)) + } + + pub fn par_iter_files_mut(&mut self) -> impl ParallelIterator { + let filter = self.filter.clone(); + let filter_selected = self.filter_selected; + let show_hidden = self.show_hidden; + + self.files + .par_iter_mut() + .enumerate() + .filter(move |(_,f)| + f.kind == Kind::Placeholder || + !(filter.is_some() && + !f.name.contains(filter.as_ref().unwrap())) && + (!filter_selected || f.selected)) + .filter(move |(_,f)| !(!show_hidden && f.hidden)) + } pub fn iter_files(&self) -> impl Iterator { let filter = self.filter.clone(); let filter_selected = self.filter_selected; @@ -440,7 +472,7 @@ impl Files { match self.sort { SortBy::Name => self .files - .sort_unstable_by(|a, b| { + .par_sort_unstable_by(|a, b| { if dirs_first { match (a.is_dir(), b.is_dir()) { (true, false) => Less, @@ -456,7 +488,7 @@ impl Files { self.meta_all_sync().log(); } - self.files.sort_unstable_by(|a, b| { + self.files.par_sort_unstable_by(|a, b| { if dirs_first { match (a.is_dir(), b.is_dir()) { (true, false) => return Less, @@ -482,7 +514,7 @@ impl Files { self.meta_all_sync().log(); } - self.files.sort_unstable_by(|a, b| { + self.files.par_sort_unstable_by(|a, b| { if dirs_first { match (a.is_dir(), b.is_dir()) { (true, false) => return Less,