@@ -109,7 +109,7 @@ def run_benchmarks(
109
109
110
110
return function_benchmark_timings , total_benchmark_timings
111
111
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 ]:
113
113
"""Discover functions to optimize."""
114
114
from codeflash .discovery .functions_to_optimize import get_functions_to_optimize
115
115
@@ -255,7 +255,7 @@ def run(self) -> None:
255
255
cleanup_paths (Optimizer .find_leftover_instrumented_test_files (self .test_cfg .tests_root ))
256
256
257
257
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 ()
259
259
function_benchmark_timings , total_benchmark_timings = self .run_benchmarks (
260
260
file_to_funcs_to_optimize , num_optimizable_functions
261
261
)
@@ -282,7 +282,21 @@ def run(self) -> None:
282
282
283
283
validated_original_code , original_module_ast = module_prep_result
284
284
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 :
286
300
function_iterator_count += 1
287
301
logger .info (
288
302
f"Optimizing function { function_iterator_count } of { num_optimizable_functions } : "
@@ -322,6 +336,8 @@ def run(self) -> None:
322
336
ph ("cli-optimize-run-finished" , {"optimizations_found" : optimizations_found })
323
337
if self .functions_checkpoint :
324
338
self .functions_checkpoint .cleanup ()
339
+ if hasattr (self .args , "command" ) and self .args .command == "optimize" :
340
+ self .cleanup_replay_tests ()
325
341
if optimizations_found == 0 :
326
342
logger .info ("❌ No optimizations found." )
327
343
elif self .args .all :
@@ -352,6 +368,11 @@ def find_leftover_instrumented_test_files(test_root: Path) -> list[Path]:
352
368
file_path for file_path in test_root .rglob ("*" ) if file_path .is_file () and pattern .match (file_path .name )
353
369
]
354
370
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
+
355
376
def cleanup_temporary_paths (self ) -> None :
356
377
if self .current_function_optimizer :
357
378
self .current_function_optimizer .cleanup_generated_files ()
0 commit comments