|
| 1 | +import contextlib |
| 2 | +import os |
| 3 | +from pathlib import Path |
| 4 | + |
| 5 | +from codeflash.cli_cmds.console import code_print |
| 6 | +from codeflash.code_utils.git_worktree_utils import create_diff_patch_from_worktree |
| 7 | +from codeflash.either import is_successful |
| 8 | +from codeflash.lsp.server import CodeflashLanguageServer |
| 9 | + |
| 10 | + |
| 11 | +# ruff: noqa: PLR0911, ANN001 |
| 12 | +def sync_perform_optimization(server: CodeflashLanguageServer, params) -> dict[str, str]: |
| 13 | + 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() |
| 51 | + |
| 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 | + |
| 58 | + optimizable_funcs = {current_function.file_path: [current_function]} |
| 59 | + |
| 60 | + devnull_writer = open(os.devnull, "w") # noqa |
| 61 | + with contextlib.redirect_stdout(devnull_writer): |
| 62 | + function_to_tests, _num_discovered_tests = server.optimizer.discover_tests(optimizable_funcs) |
| 63 | + function_optimizer.function_to_tests = function_to_tests |
| 64 | + |
| 65 | + test_setup_result = function_optimizer.generate_and_instrument_tests( |
| 66 | + code_context, should_run_experiment=should_run_experiment |
| 67 | + ) |
| 68 | + if not is_successful(test_setup_result): |
| 69 | + return {"functionName": params.functionName, "status": "error", "message": test_setup_result.failure()} |
| 70 | + ( |
| 71 | + generated_tests, |
| 72 | + function_to_concolic_tests, |
| 73 | + concolic_test_str, |
| 74 | + optimizations_set, |
| 75 | + generated_test_paths, |
| 76 | + generated_perf_test_paths, |
| 77 | + instrumented_unittests_created_for_function, |
| 78 | + original_conftest_content, |
| 79 | + ) = test_setup_result.unwrap() |
| 80 | + |
| 81 | + baseline_setup_result = function_optimizer.setup_and_establish_baseline( |
| 82 | + code_context=code_context, |
| 83 | + original_helper_code=original_helper_code, |
| 84 | + function_to_concolic_tests=function_to_concolic_tests, |
| 85 | + generated_test_paths=generated_test_paths, |
| 86 | + generated_perf_test_paths=generated_perf_test_paths, |
| 87 | + instrumented_unittests_created_for_function=instrumented_unittests_created_for_function, |
| 88 | + original_conftest_content=original_conftest_content, |
| 89 | + ) |
| 90 | + |
| 91 | + if not is_successful(baseline_setup_result): |
| 92 | + return {"functionName": params.functionName, "status": "error", "message": baseline_setup_result.failure()} |
| 93 | + |
| 94 | + ( |
| 95 | + function_to_optimize_qualified_name, |
| 96 | + function_to_all_tests, |
| 97 | + original_code_baseline, |
| 98 | + test_functions_to_remove, |
| 99 | + file_path_to_helper_classes, |
| 100 | + ) = baseline_setup_result.unwrap() |
| 101 | + |
| 102 | + best_optimization = function_optimizer.find_and_process_best_optimization( |
| 103 | + optimizations_set=optimizations_set, |
| 104 | + code_context=code_context, |
| 105 | + original_code_baseline=original_code_baseline, |
| 106 | + original_helper_code=original_helper_code, |
| 107 | + file_path_to_helper_classes=file_path_to_helper_classes, |
| 108 | + function_to_optimize_qualified_name=function_to_optimize_qualified_name, |
| 109 | + function_to_all_tests=function_to_all_tests, |
| 110 | + generated_tests=generated_tests, |
| 111 | + test_functions_to_remove=test_functions_to_remove, |
| 112 | + concolic_test_str=concolic_test_str, |
| 113 | + ) |
| 114 | + |
| 115 | + if not best_optimization: |
| 116 | + server.show_message_log( |
| 117 | + f"No best optimizations found for function {function_to_optimize_qualified_name}", "Warning" |
| 118 | + ) |
| 119 | + return { |
| 120 | + "functionName": params.functionName, |
| 121 | + "status": "error", |
| 122 | + "message": f"No best optimizations found for function {function_to_optimize_qualified_name}", |
| 123 | + } |
| 124 | + # generate a patch for the optimization |
| 125 | + relative_file_paths = [code_string.file_path for code_string in code_context.read_writable_code.code_strings] |
| 126 | + 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 | + }, |
| 141 | + ) |
| 142 | + |
| 143 | + server.show_message_log(f"Optimization completed for {params.functionName} with {speedup:.2f}x speedup", "Info") |
| 144 | + return { |
| 145 | + "functionName": params.functionName, |
| 146 | + "status": "success", |
| 147 | + "message": "Optimization completed successfully", |
| 148 | + "extra": f"Speedup: {speedup:.2f}x faster", |
| 149 | + "patch_file": metadata["patch_path"], |
| 150 | + "patch_id": metadata["id"], |
| 151 | + "explanation": best_optimization.explanation_v2, |
| 152 | + } |
0 commit comments