@@ -314,16 +314,30 @@ def closest_matching_file_function_name(
314314def 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
329343def get_functions_inside_a_commit (commit_hash : str ) -> dict [str , list [FunctionToOptimize ]]:
0 commit comments