From 5628adb982be59bb518da7884fa0e8bebfa88016 Mon Sep 17 00:00:00 2001 From: Max Bachmann Date: Sun, 31 Dec 2023 21:10:49 +0100 Subject: [PATCH] reduce binary size of levenshtein 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 --- CHANGELOG.md | 2 ++ src/lib.rs | 8 ++------ 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ebde8cd..214e441 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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. diff --git a/src/lib.rs b/src/lib.rs index 16e15d2..f010bc4 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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 = (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));