Skip to content

Commit 12df98c

Browse files
Merge branch 'main' into e2e/worktree-test
2 parents e77eecf + 7b203a3 commit 12df98c

File tree

1 file changed

+17
-56
lines changed

1 file changed

+17
-56
lines changed

codeflash/lsp/features/perform_optimization.py

Lines changed: 17 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,17 @@
11
import contextlib
22
import os
3-
from pathlib import Path
43

54
from codeflash.cli_cmds.console import code_print
65
from codeflash.code_utils.git_worktree_utils import create_diff_patch_from_worktree
76
from codeflash.either import is_successful
87
from codeflash.lsp.server import CodeflashLanguageServer
98

109

11-
# ruff: noqa: PLR0911, ANN001
12-
def sync_perform_optimization(server: CodeflashLanguageServer, params) -> dict[str, str]:
10+
def sync_perform_optimization(server: CodeflashLanguageServer, params) -> dict[str, str]: # noqa: ANN001
1311
server.show_message_log(f"Starting optimization for function: {params.functionName}", "Info")
14-
current_function = server.optimizer.current_function_being_optimized
15-
16-
if not current_function:
17-
server.show_message_log(f"No current function being optimized for {params.functionName}", "Error")
18-
return {
19-
"functionName": params.functionName,
20-
"status": "error",
21-
"message": "No function currently being optimized",
22-
}
23-
24-
module_prep_result = server.optimizer.prepare_module_for_optimization(current_function.file_path)
25-
if not module_prep_result:
26-
return {
27-
"functionName": params.functionName,
28-
"status": "error",
29-
"message": "Failed to prepare module for optimization",
30-
}
31-
32-
validated_original_code, original_module_ast = module_prep_result
33-
34-
function_optimizer = server.optimizer.create_function_optimizer(
35-
current_function,
36-
function_to_optimize_source_code=validated_original_code[current_function.file_path].source_code,
37-
original_module_ast=original_module_ast,
38-
original_module_path=current_function.file_path,
39-
function_to_tests={},
40-
)
41-
42-
server.optimizer.current_function_optimizer = function_optimizer
43-
if not function_optimizer:
44-
return {"functionName": params.functionName, "status": "error", "message": "No function optimizer found"}
45-
46-
initialization_result = function_optimizer.can_be_optimized()
47-
if not is_successful(initialization_result):
48-
return {"functionName": params.functionName, "status": "error", "message": initialization_result.failure()}
49-
50-
should_run_experiment, code_context, original_helper_code = initialization_result.unwrap()
12+
should_run_experiment, code_context, original_helper_code = server.current_optimization_init_result
13+
function_optimizer = server.optimizer.current_function_optimizer
14+
current_function = function_optimizer.function_to_optimize
5115

5216
code_print(
5317
code_context.read_writable_code.flat,
@@ -124,29 +88,26 @@ def sync_perform_optimization(server: CodeflashLanguageServer, params) -> dict[s
12488
# generate a patch for the optimization
12589
relative_file_paths = [code_string.file_path for code_string in code_context.read_writable_code.code_strings]
12690
speedup = original_code_baseline.runtime / best_optimization.runtime
127-
# get the original file path in the actual project (not in the worktree)
128-
original_args, _ = server.optimizer.original_args_and_test_cfg
129-
relative_file_path = current_function.file_path.relative_to(server.optimizer.current_worktree)
130-
original_file_path = Path(original_args.project_root / relative_file_path).resolve()
131-
132-
metadata = create_diff_patch_from_worktree(
133-
server.optimizer.current_worktree,
134-
relative_file_paths,
135-
metadata_input={
136-
"fto_name": function_to_optimize_qualified_name,
137-
"explanation": best_optimization.explanation_v2,
138-
"file_path": str(original_file_path),
139-
"speedup": speedup,
140-
},
91+
92+
patch_path = create_diff_patch_from_worktree(
93+
server.optimizer.current_worktree, relative_file_paths, function_to_optimize_qualified_name
14194
)
14295

96+
if not patch_path:
97+
return {
98+
"functionName": params.functionName,
99+
"status": "error",
100+
"message": "Failed to create a patch for optimization",
101+
}
102+
143103
server.show_message_log(f"Optimization completed for {params.functionName} with {speedup:.2f}x speedup", "Info")
104+
144105
return {
145106
"functionName": params.functionName,
146107
"status": "success",
147108
"message": "Optimization completed successfully",
148109
"extra": f"Speedup: {speedup:.2f}x faster",
149-
"patch_file": metadata["patch_path"],
150-
"patch_id": metadata["id"],
110+
"patch_file": str(patch_path),
111+
"task_id": params.task_id,
151112
"explanation": best_optimization.explanation_v2,
152113
}

0 commit comments

Comments
 (0)