Skip to content

Commit 95626cb

Browse files
Merge pull request #927 from codeflash-ai/codeflash/optimize-pr924-2025-11-17T17.24.40
⚡️ Speed up function `levenshtein_distance` by 12% in PR #924 (`small-fixes`)
2 parents 3b2fa76 + 5ba76b5 commit 95626cb

File tree

1 file changed

+22
-8
lines changed

1 file changed

+22
-8
lines changed

codeflash/discovery/functions_to_optimize.py

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -314,16 +314,30 @@ def closest_matching_file_function_name(
314314
def levenshtein_distance(s1: str, s2: str):
315315
if len(s1) > len(s2):
316316
s1, s2 = s2, s1
317-
distances = range(len(s1) + 1)
318-
for index2, char2 in enumerate(s2):
319-
newDistances = [index2 + 1]
320-
for index1, char1 in enumerate(s1):
317+
len1 = len(s1)
318+
len2 = len(s2)
319+
# Use a preallocated list instead of creating a new list every iteration
320+
previous = list(range(len1 + 1))
321+
current = [0] * (len1 + 1)
322+
323+
for index2 in range(len2):
324+
char2 = s2[index2]
325+
current[0] = index2 + 1
326+
for index1 in range(len1):
327+
char1 = s1[index1]
321328
if char1 == char2:
322-
newDistances.append(distances[index1])
329+
current[index1 + 1] = previous[index1]
323330
else:
324-
newDistances.append(1 + min((distances[index1], distances[index1 + 1], newDistances[-1])))
325-
distances = newDistances
326-
return distances[-1]
331+
# Fast min calculation without tuple construct
332+
a = previous[index1]
333+
b = previous[index1 + 1]
334+
c = current[index1]
335+
min_val = min(b, a)
336+
min_val = min(c, min_val)
337+
current[index1 + 1] = 1 + min_val
338+
# Swap references instead of copying
339+
previous, current = current, previous
340+
return previous[len1]
327341

328342

329343
def get_functions_inside_a_commit(commit_hash: str) -> dict[str, list[FunctionToOptimize]]:

0 commit comments

Comments
 (0)