perf: add instance prop of SkimMatcherV2

This is so we are not crating an new instance of this each time we are
scoring a match.
This commit is contained in:
Ade Attwood 2022-08-25 19:42:22 +01:00
parent 9fdb633f3e
commit f4a65a574c
3 changed files with 11 additions and 4 deletions

View file

@ -41,7 +41,7 @@ pub extern "C" fn ivy_match(c_pattern: *const c_char, c_text: *const c_char) ->
let pattern = to_string(c_pattern); let pattern = to_string(c_pattern);
let text = to_string(c_text); let text = to_string(c_text);
let m = matcher::Matcher{ pattern }; let m = matcher::Matcher::new( pattern );
return m.score(text) as i32; return m.score(text) as i32;
} }

View file

@ -4,12 +4,19 @@ use fuzzy_matcher::skim::SkimMatcherV2;
pub struct Matcher { pub struct Matcher {
/// The search pattern that we want to match against some text /// The search pattern that we want to match against some text
pub pattern: String, pub pattern: String,
matcher: SkimMatcherV2,
} }
impl Matcher { impl Matcher {
pub fn new(pattern: String) -> Self {
return Self {
pattern,
matcher: SkimMatcherV2::default(),
}
}
pub fn score(self: &Self, text: String) -> i64 { pub fn score(self: &Self, text: String) -> i64 {
let matcher = SkimMatcherV2::default(); if let Some((score, _indices)) = self.matcher.fuzzy_indices(&text, &self.pattern) {
if let Some((score, _indices)) = matcher.fuzzy_indices(&text, &self.pattern) {
return score; return score;
} }

View file

@ -23,7 +23,7 @@ impl Options {
pub fn sort_strings(options: Options, strings: Vec<String>) -> Arc<Mutex<Vec<Match>>> { pub fn sort_strings(options: Options, strings: Vec<String>) -> Arc<Mutex<Vec<Match>>> {
let matches: Arc<Mutex<Vec<Match>>> = Arc::new(Mutex::new(Vec::new())); let matches: Arc<Mutex<Vec<Match>>> = Arc::new(Mutex::new(Vec::new()));
let matcher = Arc::new(Mutex::new(matcher::Matcher{ pattern: options.pattern })); let matcher = Arc::new(Mutex::new(matcher::Matcher::new(options.pattern)));
let pool = thread_pool::ThreadPool::new(std::thread::available_parallelism().unwrap().get()); let pool = thread_pool::ThreadPool::new(std::thread::available_parallelism().unwrap().get());