Skip to content

Commit

Permalink
raw_detect perf tweaks
Browse files Browse the repository at this point in the history
- applied clippy::needless_for_each lint (see https://rust-lang.github.io/rust-clippy/master/index.html#needless_for_each)
- declare scores vec with_capacity to minimize allocs
- explicitly declared vars (benchmarks show it helped)
  • Loading branch information
jqnatividad committed Oct 1, 2023
1 parent 4a3da51 commit 1bbe2c4
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions src/combined/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,25 +33,25 @@ pub fn detect(iquery: &InternalQuery) -> Option<Info> {

// TODO: optimize!
pub fn raw_detect(iquery: &InternalQuery) -> RawOutcome {
let alphabet_raw_outcome = alphabets::raw_detect(iquery);
let trigram_raw_outcome = trigrams::raw_detect(iquery);
let alphabet_raw_outcome: alphabets::RawOutcome = alphabets::raw_detect(iquery);
let trigram_raw_outcome: trigrams::RawOutcome = trigrams::raw_detect(iquery);

let alphabet_scores = &alphabet_raw_outcome.scores;
let trigram_scores = &trigram_raw_outcome.scores;
let alphabet_scores: &Vec<(Lang, f64)> = &alphabet_raw_outcome.scores;
let trigram_scores: &Vec<(Lang, f64)> = &trigram_raw_outcome.scores;

let mut all_langs: Vec<Lang> = alphabet_scores.iter().map(|x| x.0).collect();
trigram_scores.iter().for_each(|(lang, _)| {
for (lang, _) in trigram_scores.iter() {
if !all_langs.contains(lang) {
all_langs.push(*lang);
}
});
}

let count = alphabet_raw_outcome.count;

let alphabet_weight = calc_alphabet_weight(count);
let trigram_weight = 1.0 - alphabet_weight;

let mut scores = vec![];
let mut scores = Vec::with_capacity(alphabet_scores.len() + trigram_scores.len());

for lang in all_langs {
let a: f64 = alphabet_scores
Expand Down

0 comments on commit 1bbe2c4

Please sign in to comment.