Skip to content

Commit c4af0b9

Browse files
committed
Merge remote-tracking branch 'origin/small-fixes' into small-fixes
# Conflicts: # codeflash/discovery/functions_to_optimize.py
2 parents 3a058a0 + 95626cb commit c4af0b9

File tree

1 file changed

+30
-10
lines changed

1 file changed

+30
-10
lines changed

codeflash/discovery/functions_to_optimize.py

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -284,13 +284,19 @@ def closest_matching_file_function_name(
284284
closest_match = None
285285
closest_file = None
286286

287-
qualified_fn_to_find = qualified_fn_to_find.lower()
287+
qualified_fn_to_find_lower = qualified_fn_to_find.lower()
288+
289+
# Cache levenshtein_distance locally for improved lookup speed
290+
_levenshtein = levenshtein_distance
288291

289292
for file_path, functions in found_fns.items():
290293
for function in functions:
291294
# Compare either full qualified name or just function name
292295
fn_name = function.qualified_name.lower()
293-
dist = levenshtein_distance(qualified_fn_to_find, fn_name)
296+
# If the absolute length difference is already >= min_distance, skip calculation
297+
if abs(len(qualified_fn_to_find_lower) - len(fn_name)) >= min_distance:
298+
continue
299+
dist = _levenshtein(qualified_fn_to_find_lower, fn_name)
294300

295301
if dist < min_distance:
296302
min_distance = dist
@@ -305,16 +311,30 @@ def closest_matching_file_function_name(
305311
def levenshtein_distance(s1: str, s2: str) -> int:
306312
if len(s1) > len(s2):
307313
s1, s2 = s2, s1
308-
distances = range(len(s1) + 1)
309-
for index2, char2 in enumerate(s2):
310-
new_distances = [index2 + 1]
311-
for index1, char1 in enumerate(s1):
314+
len1 = len(s1)
315+
len2 = len(s2)
316+
# Use a preallocated list instead of creating a new list every iteration
317+
previous = list(range(len1 + 1))
318+
current = [0] * (len1 + 1)
319+
320+
for index2 in range(len2):
321+
char2 = s2[index2]
322+
current[0] = index2 + 1
323+
for index1 in range(len1):
324+
char1 = s1[index1]
312325
if char1 == char2:
313-
new_distances.append(distances[index1])
326+
current[index1 + 1] = previous[index1]
314327
else:
315-
new_distances.append(1 + min((distances[index1], distances[index1 + 1], new_distances[-1])))
316-
distances = new_distances
317-
return distances[-1]
328+
# Fast min calculation without tuple construct
329+
a = previous[index1]
330+
b = previous[index1 + 1]
331+
c = current[index1]
332+
min_val = min(b, a)
333+
min_val = min(c, min_val)
334+
current[index1 + 1] = 1 + min_val
335+
# Swap references instead of copying
336+
previous, current = current, previous
337+
return previous[len1]
318338

319339

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

0 commit comments

Comments
 (0)