Skip to content

Commit

Permalink
improve performance of transposition count for slow iterators
Browse files Browse the repository at this point in the history
  • Loading branch information
maxbachmann committed Nov 26, 2023
1 parent 54a0081 commit 25e4a38
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 11 deletions.
1 change: 1 addition & 0 deletions src/details/intrinsics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ pub const fn blsi_u64(v: u64) -> u64 {
v & v.wrapping_neg()
}

#[allow(dead_code)]
pub const fn blsr_u64(v: u64) -> u64 {
v & v.wrapping_sub(1)
}
28 changes: 17 additions & 11 deletions src/distance/jaro.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::details::common::find_common_prefix;
use crate::details::distance::Metricf64;
use crate::details::intrinsics::{bit_mask_lsb_u64, blsi_u64, blsr_u64, ceil_div_usize};
use crate::details::intrinsics::{bit_mask_lsb_u64, blsi_u64, ceil_div_usize};
use crate::details::pattern_match_vector::{
BitVectorInterface, BlockPatternMatchVector, PatternMatchVector,
};
Expand Down Expand Up @@ -275,7 +275,7 @@ where

fn count_transpositions_word<PmVec, Iter2, Elem2>(
pm: &PmVec,
s2: Iter2,
mut s2: Iter2,
_len2: usize,
flagged: &FlaggedCharsWord,
) -> usize
Expand All @@ -286,17 +286,18 @@ where
{
let mut p_flag = flagged.p_flag;
let mut t_flag = flagged.t_flag;
let mut transpositions = 0;
let mut transpositions = 0_usize;
while t_flag != 0 {
let pattern_flag_mask = blsi_u64(p_flag);

let s2_index = t_flag.trailing_zeros() as usize;
let ch2 = s2
.clone()
.nth(t_flag.trailing_zeros() as usize)
.nth(s2_index)
.expect("these can't be outside, since we set the flags based on available indexes");

transpositions += usize::from((pm.get(0, ch2) & pattern_flag_mask) == 0);

t_flag = blsr_u64(t_flag);
t_flag = (t_flag >> 1) >> s2_index;
p_flag ^= pattern_flag_mask;
}

Expand All @@ -320,28 +321,32 @@ where
let mut p_flag = flagged.p_flag[pattern_word];

let mut transpositions = 0;
let mut s2_pos = 0_usize;
while flagged_chars != 0 {
while t_flag == 0 {
text_word += 1;
s2.nth(64 - 1);
if s2_pos < 64 {
s2.nth(64 - 1 - s2_pos);
}
t_flag = flagged.t_flag[text_word];
s2_pos = 0;
}

while t_flag != 0 {
while p_flag == 0 {
pattern_word += 1;
p_flag = flagged.p_flag[pattern_word];
}

let pattern_flag_mask = blsi_u64(p_flag);

let ch2 = s2.clone().nth(t_flag.trailing_zeros() as usize).expect(
let s2_index = t_flag.trailing_zeros() as usize;
let ch2 = s2.nth(s2_index).expect(
"these can't be outside, since we set the flags based on available indexes",
);
s2_pos += s2_index + 1;

transpositions += usize::from((pm.get(pattern_word, ch2) & pattern_flag_mask) == 0);

t_flag = blsr_u64(t_flag);
t_flag = (t_flag >> 1) >> s2_index;
p_flag ^= pattern_flag_mask;
flagged_chars -= 1;
}
Expand Down Expand Up @@ -415,6 +420,7 @@ where
} else if len1 <= 64 && len2 <= 64 {
let mut pm = PatternMatchVector::default();
pm.insert(s1_iter);

let flagged = flag_similar_characters_word(&pm, len1, s2_iter.clone(), len2, bound);

common_chars += flagged.count_common_chars();
Expand Down

0 comments on commit 25e4a38

Please sign in to comment.