diff --git a/raptor/src/rank/exact.rs b/raptor/src/rank/exact.rs index a0a480e80..6fec39696 100644 --- a/raptor/src/rank/exact.rs +++ b/raptor/src/rank/exact.rs @@ -3,11 +3,20 @@ use Match; use rank::{match_query_index, Document}; use group_by::GroupBy; -pub fn exact(lhs: &Document, rhs: &Document) -> Ordering { - let contains_exact = |matches: &[Match]| matches.iter().any(|m| m.is_exact); - let key = |matches: &[Match]| -> usize { - GroupBy::new(matches, match_query_index).map(contains_exact).filter(Clone::clone).count() - }; - - key(&lhs.matches).cmp(&key(&rhs.matches)) +#[inline] +fn contains_exact(matches: &[Match]) -> bool { + matches.iter().any(|m| m.is_exact) +} + +#[inline] +fn number_exact_matches(matches: &[Match]) -> usize { + GroupBy::new(matches, match_query_index).map(contains_exact).count() +} + +#[inline] +pub fn exact(lhs: &Document, rhs: &Document) -> Ordering { + let lhs = number_exact_matches(&lhs.matches); + let rhs = number_exact_matches(&rhs.matches); + + lhs.cmp(&rhs) } diff --git a/raptor/src/rank/mod.rs b/raptor/src/rank/mod.rs index 54ef0a391..e86f88fa4 100644 --- a/raptor/src/rank/mod.rs +++ b/raptor/src/rank/mod.rs @@ -14,12 +14,14 @@ use metadata::{DocIndexes, OpWithStateBuilder, UnionWithState}; use {Match, DocumentId}; use group_by::GroupByMut; -use self::sum_of_typos::sum_of_typos; -use self::number_of_words::number_of_words; -use self::words_proximity::words_proximity; -use self::sum_of_words_attribute::sum_of_words_attribute; -use self::sum_of_words_position::sum_of_words_position; -use self::exact::exact; +use self::{ + sum_of_typos::sum_of_typos, + number_of_words::number_of_words, + words_proximity::words_proximity, + sum_of_words_attribute::sum_of_words_attribute, + sum_of_words_position::sum_of_words_position, + exact::exact, +}; #[inline] fn match_query_index(a: &Match, b: &Match) -> bool { diff --git a/raptor/src/rank/number_of_words.rs b/raptor/src/rank/number_of_words.rs index 947bb463e..02066cf6f 100644 --- a/raptor/src/rank/number_of_words.rs +++ b/raptor/src/rank/number_of_words.rs @@ -3,10 +3,15 @@ use Match; use rank::{match_query_index, Document}; use group_by::GroupBy; -pub fn number_of_words(lhs: &Document, rhs: &Document) -> Ordering { - let key = |matches: &[Match]| -> usize { - GroupBy::new(matches, match_query_index).count() - }; - - key(&lhs.matches).cmp(&key(&rhs.matches)).reverse() +#[inline] +fn number_of_query_words(matches: &[Match]) -> usize { + GroupBy::new(matches, match_query_index).count() +} + +#[inline] +pub fn number_of_words(lhs: &Document, rhs: &Document) -> Ordering { + let lhs = number_of_query_words(&lhs.matches); + let rhs = number_of_query_words(&rhs.matches); + + lhs.cmp(&rhs).reverse() } diff --git a/raptor/src/rank/sum_of_typos.rs b/raptor/src/rank/sum_of_typos.rs index ef719eea6..f4bc151c8 100644 --- a/raptor/src/rank/sum_of_typos.rs +++ b/raptor/src/rank/sum_of_typos.rs @@ -3,10 +3,19 @@ use Match; use rank::{match_query_index, Document}; use group_by::GroupBy; -pub fn sum_of_typos(lhs: &Document, rhs: &Document) -> Ordering { - let key = |matches: &[Match]| -> u8 { - GroupBy::new(matches, match_query_index).map(|m| m[0].distance).sum() - }; - - key(&lhs.matches).cmp(&key(&rhs.matches)) +#[inline] +fn sum_matches_typos(matches: &[Match]) -> u8 { + // note that GroupBy will never return an empty group + // so we can do this assumption safely + GroupBy::new(matches, match_query_index).map(|group| unsafe { + group.get_unchecked(0).distance + }).sum() +} + +#[inline] +pub fn sum_of_typos(lhs: &Document, rhs: &Document) -> Ordering { + let lhs = sum_matches_typos(&lhs.matches); + let rhs = sum_matches_typos(&rhs.matches); + + lhs.cmp(&rhs) } diff --git a/raptor/src/rank/sum_of_words_attribute.rs b/raptor/src/rank/sum_of_words_attribute.rs index 8731a9c54..eddd8a009 100644 --- a/raptor/src/rank/sum_of_words_attribute.rs +++ b/raptor/src/rank/sum_of_words_attribute.rs @@ -3,10 +3,19 @@ use Match; use rank::{match_query_index, Document}; use group_by::GroupBy; -pub fn sum_of_words_attribute(lhs: &Document, rhs: &Document) -> Ordering { - let key = |matches: &[Match]| -> u8 { - GroupBy::new(matches, match_query_index).map(|m| m[0].attribute).sum() - }; - - key(&lhs.matches).cmp(&key(&rhs.matches)) +#[inline] +fn sum_matches_attributes(matches: &[Match]) -> u8 { + // note that GroupBy will never return an empty group + // so we can do this assumption safely + GroupBy::new(matches, match_query_index).map(|group| unsafe { + group.get_unchecked(0).attribute + }).sum() +} + +#[inline] +pub fn sum_of_words_attribute(lhs: &Document, rhs: &Document) -> Ordering { + let lhs = sum_matches_attributes(&lhs.matches); + let rhs = sum_matches_attributes(&rhs.matches); + + lhs.cmp(&rhs) } diff --git a/raptor/src/rank/sum_of_words_position.rs b/raptor/src/rank/sum_of_words_position.rs index dc523bb8f..1aab9eff4 100644 --- a/raptor/src/rank/sum_of_words_position.rs +++ b/raptor/src/rank/sum_of_words_position.rs @@ -3,10 +3,19 @@ use Match; use rank::{match_query_index, Document}; use group_by::GroupBy; -fn key(matches: &[Match]) -> u32 { - GroupBy::new(matches, match_query_index).map(|m| m[0].attribute_index).sum() +#[inline] +fn sum_matches_attribute_index(matches: &[Match]) -> u32 { + // note that GroupBy will never return an empty group + // so we can do this assumption safely + GroupBy::new(matches, match_query_index).map(|group| unsafe { + group.get_unchecked(0).attribute_index + }).sum() } +#[inline] pub fn sum_of_words_position(lhs: &Document, rhs: &Document) -> Ordering { - key(&lhs.matches).cmp(&key(&rhs.matches)) + let lhs = sum_matches_attribute_index(&lhs.matches); + let rhs = sum_matches_attribute_index(&rhs.matches); + + lhs.cmp(&rhs) }