charselect: parallelize fuzzy matching
This gets the matching time down below 110+ms to around 70ms in release mode, which may not sound like a huge amount, but 100ms is on the cusp of perceivable latency, so getting below that is desirable.
This commit is contained in:
3 files changed
+56
-42
No files matched your search
Generated
+1
@@ -6164,6 +6164,7 @@ dependencies = [
|
||||
"pulldown-cmark",
|
||||
"rangeset",
|
||||
"ratelim",
|
||||
"rayon",
|
||||
"regex",
|
||||
"serde",
|
||||
"serde_json",
|
||||
|
||||
@@ -73,6 +73,7 @@ promise = { path = "../promise" }
|
||||
pulldown-cmark = "0.9"
|
||||
rangeset = { path = "../rangeset" }
|
||||
ratelim= { path = "../ratelim" }
|
||||
rayon = "1.7"
|
||||
regex = "1"
|
||||
serde = {version="1.0", features = ["rc", "derive"]}
|
||||
serde_json = "1.0"
|
||||
|
||||
@@ -15,6 +15,7 @@ use emojis::{Emoji, Group};
|
||||
use frecency::Frecency;
|
||||
use fuzzy_matcher::skim::SkimMatcherV2;
|
||||
use fuzzy_matcher::FuzzyMatcher;
|
||||
use rayon::prelude::*;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::borrow::Cow;
|
||||
use std::cell::{Ref, RefCell};
|
||||
@@ -226,7 +227,7 @@ fn build_aliases() -> Vec<Alias> {
|
||||
aliases
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
#[derive(Debug, Copy, Clone)]
|
||||
struct MatchResult {
|
||||
row_idx: usize,
|
||||
score: i64,
|
||||
@@ -269,56 +270,67 @@ fn compute_matches(selection: &str, aliases: &[Alias], group: CharSelectGroup) -
|
||||
} else {
|
||||
None
|
||||
};
|
||||
// Use a hash map to remove duplicates, as the same emoji may match multiple times with a different name.
|
||||
let mut matches = HashMap::<String, MatchResult>::new();
|
||||
|
||||
let start = std::time::Instant::now();
|
||||
for (row_idx, entry) in aliases.iter().enumerate() {
|
||||
let glyph = entry.glyph();
|
||||
let alias_result = matcher
|
||||
.fuzzy_match(&entry.name, selection)
|
||||
.map(|score| MatchResult::new(row_idx, score, selection, aliases));
|
||||
if let Some(value) = match &numeric_selection {
|
||||
Some(sel) => {
|
||||
let codepoints = entry.codepoints();
|
||||
if codepoints == *sel {
|
||||
Some(MatchResult {
|
||||
row_idx,
|
||||
score: i64::max_value(),
|
||||
})
|
||||
} else {
|
||||
let number_result = matcher
|
||||
.fuzzy_match(&codepoints, &sel)
|
||||
.map(|score| MatchResult::new(row_idx, score, sel, aliases));
|
||||
let all_matches: Vec<(String, MatchResult)> = aliases
|
||||
.par_iter()
|
||||
.enumerate()
|
||||
.filter_map(|(row_idx, entry)| {
|
||||
let glyph = entry.glyph();
|
||||
let alias_result = matcher
|
||||
.fuzzy_match(&entry.name, selection)
|
||||
.map(|score| MatchResult::new(row_idx, score, selection, aliases));
|
||||
match &numeric_selection {
|
||||
Some(sel) => {
|
||||
let codepoints = entry.codepoints();
|
||||
if codepoints == *sel {
|
||||
Some((
|
||||
glyph,
|
||||
MatchResult {
|
||||
row_idx,
|
||||
score: i64::max_value(),
|
||||
},
|
||||
))
|
||||
} else {
|
||||
let number_result = matcher
|
||||
.fuzzy_match(&codepoints, &sel)
|
||||
.map(|score| MatchResult::new(row_idx, score, sel, aliases));
|
||||
|
||||
match (alias_result, number_result) {
|
||||
(
|
||||
Some(MatchResult { score: a, .. }),
|
||||
Some(MatchResult { score: b, .. }),
|
||||
) => Some(MatchResult {
|
||||
row_idx,
|
||||
score: a.max(b),
|
||||
}),
|
||||
(Some(a), None) | (None, Some(a)) => Some(a),
|
||||
(None, None) => None,
|
||||
match (alias_result, number_result) {
|
||||
(
|
||||
Some(MatchResult { score: a, .. }),
|
||||
Some(MatchResult { score: b, .. }),
|
||||
) => Some((
|
||||
glyph,
|
||||
MatchResult {
|
||||
row_idx,
|
||||
score: a.max(b),
|
||||
},
|
||||
)),
|
||||
(Some(a), None) | (None, Some(a)) => Some((glyph, a)),
|
||||
(None, None) => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
None => alias_result.map(|a| (glyph, a)),
|
||||
}
|
||||
None => alias_result,
|
||||
} {
|
||||
if let Some(other) = matches.get_mut(&glyph) {
|
||||
// if the character was inserted already, replace it only if the score is improved
|
||||
if other.score < value.score {
|
||||
*other = value;
|
||||
}
|
||||
} else {
|
||||
matches.insert(glyph, value);
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
|
||||
let mut matches = HashMap::<String, MatchResult>::new();
|
||||
for (glyph, value) in all_matches {
|
||||
let entry = matches.entry(glyph).or_insert(value);
|
||||
// Retain the best scoring match for a given glyph
|
||||
if entry.score < value.score {
|
||||
*entry = value;
|
||||
}
|
||||
}
|
||||
let mut scores: Vec<MatchResult> = matches.into_values().collect();
|
||||
scores.sort_by(|a, b| a.score.cmp(&b.score).reverse());
|
||||
log::trace!("matching took {:?}", start.elapsed());
|
||||
log::trace!(
|
||||
"matching took {:?} for {} entries",
|
||||
start.elapsed(),
|
||||
scores.len()
|
||||
);
|
||||
|
||||
scores.iter().map(|result| result.row_idx).collect()
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user