1111from pydantic .json import pydantic_encoder
1212
1313from codeflash .cli_cmds .console import console , logger
14+ from codeflash .code_utils .code_replacer import is_zero_diff
15+ from codeflash .code_utils .code_utils import unified_diff_strings
1416from codeflash .code_utils .config_consts import N_CANDIDATES_EFFECTIVE , N_CANDIDATES_LP_EFFECTIVE
1517from codeflash .code_utils .env_utils import get_codeflash_api_key
1618from codeflash .code_utils .git_utils import get_last_commit_author_if_pr_exists , get_repo_owner_and_name
1719from codeflash .code_utils .time_utils import humanize_runtime
20+ from codeflash .github .PrComment import FileDiffContent
1821from codeflash .lsp .helpers import is_LSP_enabled
1922from codeflash .models .ExperimentMetadata import ExperimentMetadata
2023from codeflash .models .models import AIServiceRefinerRequest , CodeStringsMarkdown , OptimizedCandidate
@@ -529,8 +532,6 @@ def get_optimization_impact(
529532 replay_tests : str ,
530533 concolic_tests : str ,
531534 root_dir : Path ,
532- original_line_profiler_results : str ,
533- optimized_line_profiler_results : str ,
534535 ) -> str :
535536 """Optimize the given python code for performance by making a request to the Django endpoint.
536537
@@ -553,34 +554,22 @@ def get_optimization_impact(
553554 - 'high','medium' or 'low' optimization impact
554555
555556 """
557+ diff_str = '\n ' .join ([unified_diff_strings (code1 = original_code [p ], code2 = new_code [p ], fromfile = Path (p ).relative_to (root_dir ).as_posix (), tofile = Path (p ).relative_to (root_dir ).as_posix ()) for p in original_code if not is_zero_diff (original_code [p ], new_code [p ])])
558+ code_diff = f"```diff\n { diff_str } \n ```"
556559 logger .info ("!lsp|Computing Optimization Impact…" )
557- original_code_str = ""
558- new_code_str = ""
559- for p , code in original_code .items ():
560- original_code_str += f"```python:{ Path (p ).relative_to (root_dir ).as_posix ()} "
561- original_code_str += "\n "
562- original_code_str += code
563- for p , code in new_code .items ():
564- new_code_str += f"```python:{ Path (p ).relative_to (root_dir ).as_posix ()} "
565- new_code_str += "\n "
566- new_code_str += code
567-
568560 payload = {
569- "original_code" : original_code_str ,
570- "optimized_code" : new_code_str ,
561+ "code_diff" : code_diff ,
571562 "existing_tests" : existing_tests_source ,
572563 "generated_tests" : generated_original_test_source ,
573564 "trace_id" : function_trace_id ,
574565 "coverage_message" : coverage_message ,
575566 "replay_tests" : replay_tests ,
576567 "concolic_tests" : concolic_tests ,
577- "speedup" : f"{ 1 + float (explanation .speedup ):.2f} x " ,
568+ "speedup" : f"{ 100 + 100 * float (explanation .speedup ):.2f} % " ,
578569 "loop_count" : explanation .winning_benchmarking_test_results .number_of_loops (),
579570 "benchmark_details" : explanation .benchmark_details if explanation .benchmark_details else None ,
580571 "optimized_runtime" : humanize_runtime (explanation .best_runtime_ns ),
581572 "original_runtime" : humanize_runtime (explanation .original_runtime_ns ),
582- "original_line_profiler_results" : original_line_profiler_results ,
583- "optimized_line_profiler_results" : optimized_line_profiler_results ,
584573 }
585574 console .rule ()
586575 try :
0 commit comments