@@ -109,7 +109,7 @@ def run_benchmarks(
109109
110110 return function_benchmark_timings , total_benchmark_timings
111111
112- def get_optimizable_functions (self ) -> tuple [dict [Path , list [FunctionToOptimize ]], int ]:
112+ def get_optimizable_functions (self ) -> tuple [dict [Path , list [FunctionToOptimize ]], int , Path | None ]:
113113 """Discover functions to optimize."""
114114 from codeflash .discovery .functions_to_optimize import get_functions_to_optimize
115115
@@ -255,7 +255,7 @@ def run(self) -> None:
255255 cleanup_paths (Optimizer .find_leftover_instrumented_test_files (self .test_cfg .tests_root ))
256256
257257 function_optimizer = None
258- file_to_funcs_to_optimize , num_optimizable_functions = self .get_optimizable_functions ()
258+ file_to_funcs_to_optimize , num_optimizable_functions , trace_file_path = self .get_optimizable_functions ()
259259 function_benchmark_timings , total_benchmark_timings = self .run_benchmarks (
260260 file_to_funcs_to_optimize , num_optimizable_functions
261261 )
@@ -282,7 +282,21 @@ def run(self) -> None:
282282
283283 validated_original_code , original_module_ast = module_prep_result
284284
285- for function_to_optimize in file_to_funcs_to_optimize [original_module_path ]:
285+ functions_to_optimize = file_to_funcs_to_optimize [original_module_path ]
286+ if trace_file_path and trace_file_path .exists () and len (functions_to_optimize ) > 1 :
287+ try :
288+ from codeflash .benchmarking .function_ranker import FunctionRanker
289+
290+ ranker = FunctionRanker (trace_file_path )
291+ functions_to_optimize = ranker .rank_functions (functions_to_optimize )
292+ logger .info (
293+ f"Ranked { len (functions_to_optimize )} functions by performance impact in { original_module_path } "
294+ )
295+ console .rule ()
296+ except Exception as e :
297+ logger .debug (f"Could not rank functions in { original_module_path } : { e } " )
298+
299+ for function_to_optimize in functions_to_optimize :
286300 function_iterator_count += 1
287301 logger .info (
288302 f"Optimizing function { function_iterator_count } of { num_optimizable_functions } : "
@@ -322,6 +336,8 @@ def run(self) -> None:
322336 ph ("cli-optimize-run-finished" , {"optimizations_found" : optimizations_found })
323337 if self .functions_checkpoint :
324338 self .functions_checkpoint .cleanup ()
339+ if hasattr (self .args , "command" ) and self .args .command == "optimize" :
340+ self .cleanup_replay_tests ()
325341 if optimizations_found == 0 :
326342 logger .info ("❌ No optimizations found." )
327343 elif self .args .all :
@@ -352,6 +368,11 @@ def find_leftover_instrumented_test_files(test_root: Path) -> list[Path]:
352368 file_path for file_path in test_root .rglob ("*" ) if file_path .is_file () and pattern .match (file_path .name )
353369 ]
354370
371+ def cleanup_replay_tests (self ) -> None :
372+ if self .replay_tests_dir and self .replay_tests_dir .exists ():
373+ logger .debug (f"Cleaning up replay tests directory: { self .replay_tests_dir } " )
374+ cleanup_paths ([self .replay_tests_dir ])
375+
355376 def cleanup_temporary_paths (self ) -> None :
356377 if self .current_function_optimizer :
357378 self .current_function_optimizer .cleanup_generated_files ()
0 commit comments