@@ -288,31 +288,37 @@ def run(self) -> None:
288288 f"{ function_to_optimize .qualified_name } "
289289 )
290290 console .rule ()
291+ function_optimizer = None
292+ try :
293+ function_optimizer = self .create_function_optimizer (
294+ function_to_optimize ,
295+ function_to_tests = function_to_tests ,
296+ function_to_optimize_source_code = validated_original_code [original_module_path ].source_code ,
297+ function_benchmark_timings = function_benchmark_timings ,
298+ total_benchmark_timings = total_benchmark_timings ,
299+ original_module_ast = original_module_ast ,
300+ original_module_path = original_module_path ,
301+ )
291302
292- function_optimizer = self .create_function_optimizer (
293- function_to_optimize ,
294- function_to_tests = function_to_tests ,
295- function_to_optimize_source_code = validated_original_code [original_module_path ].source_code ,
296- function_benchmark_timings = function_benchmark_timings ,
297- total_benchmark_timings = total_benchmark_timings ,
298- original_module_ast = original_module_ast ,
299- original_module_path = original_module_path ,
300- )
301-
302- self .current_function_optimizer = (
303- function_optimizer # needed to clean up from the outside of this function
304- )
305- best_optimization = function_optimizer .optimize_function ()
306- if self .functions_checkpoint :
307- self .functions_checkpoint .add_function_to_checkpoint (
308- function_to_optimize .qualified_name_with_modules_from_root (self .args .project_root )
303+ self .current_function_optimizer = (
304+ function_optimizer # needed to clean up from the outside of this function
309305 )
310- if is_successful (best_optimization ):
311- optimizations_found += 1
312- else :
313- logger .warning (best_optimization .failure ())
314- console .rule ()
315- continue
306+ best_optimization = function_optimizer .optimize_function ()
307+ if self .functions_checkpoint :
308+ self .functions_checkpoint .add_function_to_checkpoint (
309+ function_to_optimize .qualified_name_with_modules_from_root (self .args .project_root )
310+ )
311+ if is_successful (best_optimization ):
312+ optimizations_found += 1
313+ else :
314+ logger .warning (best_optimization .failure ())
315+ console .rule ()
316+ continue
317+ finally :
318+ if function_optimizer is not None :
319+ function_optimizer .cleanup_generated_files ()
320+ self .cleanup_temporary_paths ()
321+
316322 ph ("cli-optimize-run-finished" , {"optimizations_found" : optimizations_found })
317323 if self .functions_checkpoint :
318324 self .functions_checkpoint .cleanup ()
0 commit comments