From c5e8677a37042c4152b3df9916725cf4ea74cf87 Mon Sep 17 00:00:00 2001 From: Xymist Date: Fri, 26 Aug 2022 16:34:15 +0100 Subject: [PATCH] Introduce Rayon for parallel iteration and sorting MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Use `into_par_iter()` before setting out to calculate scores and then filter by them This represents a more efficient parallelism approach, with no mutex or global state at top level. ivy_files(kubernetes) time: [4.5800 ms 4.6121 ms 4.6467 ms] change: [-55.056% -54.570% -54.133%] (p = 0.00 < 0.05) Performance has improved. ivy_match(file.lua) time: [1.1514 µs 1.1599 µs 1.1694 µs] change: [+0.4116% +2.0753% +3.6710%] (p = 0.01 < 0.05) Change within noise threshold. --- Cargo.lock | 1 + Cargo.toml | 1 + rust/sorter.rs | 7 ++----- 3 files changed, 4 insertions(+), 5 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 11ab482..fd5d598 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -274,6 +274,7 @@ dependencies = [ "fuzzy-matcher", "ignore", "lazy_static", + "rayon", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index 44b677a..75c6974 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,6 +12,7 @@ path = "rust/lib.rs" ignore = "0.4" fuzzy-matcher = "0.3.7" lazy_static = "1.4.0" +rayon = "1.5.3" [dev-dependencies] criterion = "0.3.6" diff --git a/rust/sorter.rs b/rust/sorter.rs index cc7c31b..71d2efa 100644 --- a/rust/sorter.rs +++ b/rust/sorter.rs @@ -1,8 +1,5 @@ use super::matcher; -use super::thread_pool; - -use std::sync::mpsc; -use std::sync::Arc; +use rayon::prelude::*; pub struct Match { pub score: i64, @@ -27,7 +24,7 @@ pub fn sort_strings(options: Options, strings: Vec) -> Vec { let matcher = matcher::Matcher::new(options.pattern); let mut matches = strings - .into_iter() + .into_par_iter() .map(|candidate| Match { score: matcher.score(candidate.as_str()), content: candidate,