Skip to content

Commit 3b2fa76

Browse files
Update codeflash/discovery/functions_to_optimize.py
Co-authored-by: codeflash-ai[bot] <148906541+codeflash-ai[bot]@users.noreply.github.com>
1 parent 626cec1 commit 3b2fa76

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

codeflash/discovery/functions_to_optimize.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -278,18 +278,28 @@ def closest_matching_file_function_name(
278278
279279
Returns:
280280
Tuple of (file_path, function) for closest match, or None if no matches found
281+
281282
"""
282283
min_distance = 4
283284
closest_match = None
284285
closest_file = None
285286

286-
qualified_fn_to_find = qualified_fn_to_find.lower()
287+
# Lowercase once before loop for better performance
288+
qualified_fn_to_find_lower = qualified_fn_to_find.lower()
289+
290+
# Cache levenshtein_distance locally for improved lookup speed
291+
_levenshtein = levenshtein_distance
292+
293+
# Use for-loop without unnecessary assignments in the inner loop
287294

288295
for file_path, functions in found_fns.items():
289296
for function in functions:
290297
# Compare either full qualified name or just function name
291298
fn_name = function.qualified_name.lower()
292-
dist = levenshtein_distance(qualified_fn_to_find, fn_name)
299+
# If the absolute length difference is already >= min_distance, skip calculation
300+
if abs(len(qualified_fn_to_find_lower) - len(fn_name)) >= min_distance:
301+
continue
302+
dist = _levenshtein(qualified_fn_to_find_lower, fn_name)
293303

294304
if dist < min_distance:
295305
min_distance = dist

0 commit comments

Comments
 (0)