Skip to content

Commit

Permalink
reduce binary size of levenshtein
Browse files Browse the repository at this point in the history
This removes the special handling for an empty first string,
which leads to an extra memory allocation in this case.
However this reduces the binary size noticeably:

opt-level=3:
961B strsim strsim::levenshtein
1.1KiB strsim strsim::levenshtein

opt-level=3
888B strsim strsim::levenshtein
949B strsim strsim::levenshtein
  • Loading branch information
maxbachmann committed Jan 4, 2024
1 parent 66822fe commit 5628adb
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 6 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ This project attempts to adhere to [Semantic Versioning](http://semver.org).
- reduce runtime
- reduce binary size by more than `25%`

- reduce binary size of Levenshtein distance

### Fixed

- Fix transposition counting in Jaro and Jaro-Winkler.
Expand Down
8 changes: 2 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,20 +225,16 @@ where
{
let b_len = b.into_iter().count();

if a.into_iter().next().is_none() {
return b_len;
}

let mut cache: Vec<usize> = (1..b_len + 1).collect();

let mut result = 0;
let mut result = b_len;

for (i, a_elem) in a.into_iter().enumerate() {
result = i + 1;
let mut distance_b = i;

for (j, b_elem) in b.into_iter().enumerate() {
let cost = if a_elem == b_elem { 0usize } else { 1usize };
let cost = usize::from(a_elem != b_elem);
let distance_a = distance_b + cost;
distance_b = cache[j];
result = min(result + 1, min(distance_a, distance_b + 1));
Expand Down

0 comments on commit 5628adb

Please sign in to comment.