@@ -36,27 +36,71 @@ def get_optimizable_functions(
36
36
server : CodeflashLanguageServer , params : OptimizableFunctionsParams
37
37
) -> dict [str , list [str ]]:
38
38
file_path = Path (uris .to_fs_path (params .textDocument .uri ))
39
- server .optimizer .args .file = file_path
40
- server .optimizer .args .previous_checkpoint_functions = False
41
- optimizable_funcs , _ = server .optimizer .get_optimizable_functions ()
42
- path_to_qualified_names = {}
43
- for path , functions in optimizable_funcs .items ():
44
- path_to_qualified_names [path .as_posix ()] = [func .qualified_name for func in functions ]
45
- return path_to_qualified_names
39
+ server .show_message_log (f"Getting optimizable functions for: { file_path } " , "Info" )
40
+
41
+ # Save original args to restore later
42
+ original_file = getattr (server .optimizer .args , "file" , None )
43
+ original_function = getattr (server .optimizer .args , "function" , None )
44
+ original_checkpoint = getattr (server .optimizer .args , "previous_checkpoint_functions" , None )
45
+
46
+ server .show_message_log (f"Original args - file: { original_file } , function: { original_function } " , "Info" )
47
+
48
+ try :
49
+ # Set temporary args for this request only
50
+ server .optimizer .args .file = file_path
51
+ server .optimizer .args .function = None # Always get ALL functions, not just one
52
+ server .optimizer .args .previous_checkpoint_functions = False
53
+
54
+ server .show_message_log ("Calling get_optimizable_functions..." , "Info" )
55
+ optimizable_funcs , _ = server .optimizer .get_optimizable_functions ()
56
+
57
+ path_to_qualified_names = {}
58
+ for path , functions in optimizable_funcs .items ():
59
+ path_to_qualified_names [path .as_posix ()] = [func .qualified_name for func in functions ]
60
+
61
+ server .show_message_log (
62
+ f"Found { len (path_to_qualified_names )} files with functions: { path_to_qualified_names } " , "Info"
63
+ )
64
+ return path_to_qualified_names
65
+ finally :
66
+ # Restore original args to prevent state corruption
67
+ if original_file is not None :
68
+ server .optimizer .args .file = original_file
69
+ if original_function is not None :
70
+ server .optimizer .args .function = original_function
71
+ else :
72
+ server .optimizer .args .function = None
73
+ if original_checkpoint is not None :
74
+ server .optimizer .args .previous_checkpoint_functions = original_checkpoint
75
+
76
+ server .show_message_log (
77
+ f"Restored args - file: { server .optimizer .args .file } , function: { server .optimizer .args .function } " , "Info"
78
+ )
46
79
47
80
48
81
@server .feature ("initializeFunctionOptimization" )
49
82
def initialize_function_optimization (
50
83
server : CodeflashLanguageServer , params : FunctionOptimizationParams
51
84
) -> dict [str , str ]:
52
85
file_path = Path (uris .to_fs_path (params .textDocument .uri ))
86
+ server .show_message_log (f"Initializing optimization for function: { params .functionName } in { file_path } " , "Info" )
87
+
88
+ # IMPORTANT: Store the specific function for optimization, but don't corrupt global state
53
89
server .optimizer .args .function = params .functionName
54
90
server .optimizer .args .file = file_path
91
+
92
+ server .show_message_log (
93
+ f"Args set - function: { server .optimizer .args .function } , file: { server .optimizer .args .file } " , "Info"
94
+ )
95
+
55
96
optimizable_funcs , _ = server .optimizer .get_optimizable_functions ()
56
97
if not optimizable_funcs :
98
+ server .show_message_log (f"No optimizable functions found for { params .functionName } " , "Warning" )
57
99
return {"functionName" : params .functionName , "status" : "not found" , "args" : None }
100
+
58
101
fto = optimizable_funcs .popitem ()[1 ][0 ]
59
102
server .optimizer .current_function_being_optimized = fto
103
+ server .show_message_log (f"Successfully initialized optimization for { params .functionName } " , "Info" )
60
104
return {"functionName" : params .functionName , "status" : "success" }
61
105
62
106
@@ -136,11 +180,20 @@ def generate_tests(server: CodeflashLanguageServer, params: FunctionOptimization
136
180
137
181
138
182
@server .feature ("performFunctionOptimization" )
139
- def perform_function_optimization (
183
+ def perform_function_optimization ( # noqa: PLR0911
140
184
server : CodeflashLanguageServer , params : FunctionOptimizationParams
141
185
) -> dict [str , str ]:
186
+ server .show_message_log (f"Starting optimization for function: { params .functionName } " , "Info" )
142
187
current_function = server .optimizer .current_function_being_optimized
143
188
189
+ if not current_function :
190
+ server .show_message_log (f"No current function being optimized for { params .functionName } " , "Error" )
191
+ return {
192
+ "functionName" : params .functionName ,
193
+ "status" : "error" ,
194
+ "message" : "No function currently being optimized" ,
195
+ }
196
+
144
197
module_prep_result = server .optimizer .prepare_module_for_optimization (current_function .file_path )
145
198
146
199
validated_original_code , original_module_ast = module_prep_result
@@ -214,19 +267,29 @@ def perform_function_optimization(
214
267
)
215
268
216
269
if not best_optimization :
270
+ server .show_message_log (
271
+ f"No best optimizations found for function { function_to_optimize_qualified_name } " , "Warning"
272
+ )
217
273
return {
218
274
"functionName" : params .functionName ,
219
275
"status" : "error" ,
220
276
"message" : f"No best optimizations found for function { function_to_optimize_qualified_name } " ,
221
277
}
222
278
223
279
optimized_source = best_optimization .candidate .source_code
280
+ speedup = original_code_baseline .runtime / best_optimization .runtime
281
+
282
+ server .show_message_log (f"Optimization completed for { params .functionName } with { speedup :.2f} x speedup" , "Info" )
283
+
284
+ # CRITICAL: Clear the function filter after optimization to prevent state corruption
285
+ server .optimizer .args .function = None
286
+ server .show_message_log ("Cleared function filter to prevent state corruption" , "Info" )
224
287
225
288
return {
226
289
"functionName" : params .functionName ,
227
290
"status" : "success" ,
228
291
"message" : "Optimization completed successfully" ,
229
- "extra" : f"Speedup: { original_code_baseline . runtime / best_optimization . runtime :.2f} x faster" ,
292
+ "extra" : f"Speedup: { speedup :.2f} x faster" ,
230
293
"optimization" : optimized_source ,
231
294
}
232
295
0 commit comments