summaryrefslogtreecommitdiff
path: root/util1.c
diff options
context:
space:
mode:
authorKenneth Finnegan <kennethfinnegan2007@gmail.com>2022-09-15 10:12:02 -0700
committerGitHub <noreply@github.com>2022-09-15 10:12:02 -0700
commit8fe8cfd60af417f5467f7723075f5ad050b806c8 (patch)
tree2b10155ae2f61267def7831ca33c29b4614510bd /util1.c
parent7a2dbf717794655a3fd82439fdb1e3d8b4730c74 (diff)
downloadrsync-8fe8cfd60af417f5467f7723075f5ad050b806c8.tar.gz
Use string length diff heuristic to skip Levenshtein Algo (#369)
When using the --fuzzy option to try and find close matches locally, the edit distance algorithm used is O(N^2), which can get painful on CPU constrained systems when working in folders with tens of thousands of files in it. The lower bound on the calculated Levenshtein distance is the difference of the two strings being compared, so if that difference is larger than the current best match, the calculation of the exact edit distance between the two strings can be skipped. Testing on the OpenSUSE package repo has shown a 50% reduction in the CPU time required to plan the rsync transaction.
Diffstat (limited to 'util1.c')
-rw-r--r--util1.c9
1 files changed, 8 insertions, 1 deletions
diff --git a/util1.c b/util1.c
index 671f3c75..da50ff1e 100644
--- a/util1.c
+++ b/util1.c
@@ -1487,12 +1487,19 @@ const char *find_filename_suffix(const char *fn, int fn_len, int *len_ptr)
#define UNIT (1 << 16)
-uint32 fuzzy_distance(const char *s1, unsigned len1, const char *s2, unsigned len2)
+uint32 fuzzy_distance(const char *s1, unsigned len1, const char *s2, unsigned len2, uint32 upperlimit)
{
uint32 a[MAXPATHLEN], diag, above, left, diag_inc, above_inc, left_inc;
int32 cost;
unsigned i1, i2;
+ /* Check to see if the Levenshtein distance must be greater than the
+ * upper limit defined by the previously found lowest distance using
+ * the heuristic that the Levenshtein distance is greater than the
+ * difference in length of the two strings */
+ if ((len1 > len2 ? len1 - len2 : len2 - len1) * UNIT > upperlimit)
+ return 0xFFFFU * UNIT + 1;
+
if (!len1 || !len2) {
if (!len1) {
s1 = s2;