22import os
33from pathlib import Path
44
5+ from codeflash .cli_cmds .console import code_print
56from codeflash .code_utils .git_worktree_utils import create_diff_patch_from_worktree
67from codeflash .either import is_successful
78from codeflash .lsp .server import CodeflashLanguageServer
89
910
1011# ruff: noqa: PLR0911, ANN001
1112def sync_perform_optimization (server : CodeflashLanguageServer , params ) -> dict [str , str ]:
13+ server .show_message_log (f"Starting optimization for function: { params .functionName } " , "Info" )
1214 current_function = server .optimizer .current_function_being_optimized
15+
1316 if not current_function :
1417 server .show_message_log (f"No current function being optimized for { params .functionName } " , "Error" )
1518 return {
@@ -35,6 +38,7 @@ def sync_perform_optimization(server: CodeflashLanguageServer, params) -> dict[s
3538 original_module_path = current_function .file_path ,
3639 function_to_tests = {},
3740 )
41+
3842 server .optimizer .current_function_optimizer = function_optimizer
3943 if not function_optimizer :
4044 return {"functionName" : params .functionName , "status" : "error" , "message" : "No function optimizer found" }
@@ -45,19 +49,24 @@ def sync_perform_optimization(server: CodeflashLanguageServer, params) -> dict[s
4549
4650 should_run_experiment , code_context , original_helper_code = initialization_result .unwrap ()
4751
48- # All the synchronous, potentially blocking calls
52+ code_print (
53+ code_context .read_writable_code .flat ,
54+ file_name = current_function .file_path ,
55+ function_name = current_function .function_name ,
56+ )
57+
4958 optimizable_funcs = {current_function .file_path : [current_function ]}
59+
5060 devnull_writer = open (os .devnull , "w" ) # noqa
5161 with contextlib .redirect_stdout (devnull_writer ):
52- function_to_tests , num_discovered_tests = server .optimizer .discover_tests (optimizable_funcs )
62+ function_to_tests , _num_discovered_tests = server .optimizer .discover_tests (optimizable_funcs )
5363 function_optimizer .function_to_tests = function_to_tests
5464
5565 test_setup_result = function_optimizer .generate_and_instrument_tests (
5666 code_context , should_run_experiment = should_run_experiment
5767 )
5868 if not is_successful (test_setup_result ):
5969 return {"functionName" : params .functionName , "status" : "error" , "message" : test_setup_result .failure ()}
60-
6170 (
6271 generated_tests ,
6372 function_to_concolic_tests ,
@@ -112,9 +121,10 @@ def sync_perform_optimization(server: CodeflashLanguageServer, params) -> dict[s
112121 "status" : "error" ,
113122 "message" : f"No best optimizations found for function { function_to_optimize_qualified_name } " ,
114123 }
115-
124+ # generate a patch for the optimization
116125 relative_file_paths = [code_string .file_path for code_string in code_context .read_writable_code .code_strings ]
117126 speedup = original_code_baseline .runtime / best_optimization .runtime
127+ # get the original file path in the actual project (not in the worktree)
118128 original_args , _ = server .optimizer .original_args_and_test_cfg
119129 relative_file_path = current_function .file_path .relative_to (server .optimizer .current_worktree )
120130 original_file_path = Path (original_args .project_root / relative_file_path ).resolve ()
@@ -129,8 +139,8 @@ def sync_perform_optimization(server: CodeflashLanguageServer, params) -> dict[s
129139 "speedup" : speedup ,
130140 },
131141 )
132- server .show_message_log (f"Optimization completed for { params .functionName } with { speedup :.2f} x speedup" , "Info" )
133142
143+ server .show_message_log (f"Optimization completed for { params .functionName } with { speedup :.2f} x speedup" , "Info" )
134144 return {
135145 "functionName" : params .functionName ,
136146 "status" : "success" ,
0 commit comments