Skip to content

Commit 64818c7

Browse files
committed
shifted cleanup
1 parent bf06c3b commit 64818c7

File tree

2 files changed

+20
-21
lines changed

2 files changed

+20
-21
lines changed

codeflash/optimization/function_optimizer.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -545,10 +545,6 @@ def replace_function_and_helpers_with_optimized_code(
545545
for helper_function in code_context.helper_functions:
546546
if helper_function.jedi_definition.type != "class":
547547
read_writable_functions_by_file_path[helper_function.file_path].add(helper_function.qualified_name)
548-
logger.debug("Read writable functions by file path:")
549-
logger.debug(read_writable_functions_by_file_path)
550-
logger.debug("Optimized code:")
551-
logger.debug(optimized_code)
552548
for module_abspath, qualified_names in read_writable_functions_by_file_path.items():
553549
did_update |= replace_function_definitions_in_module(
554550
function_names=list(qualified_names),
@@ -834,6 +830,7 @@ def establish_original_code_baseline(
834830

835831
coverage_results = None
836832
# Instrument codeflash capture
833+
original_fto_code = Path(self.function_to_optimize.file_path).read_text("utf-8")
837834
try:
838835
instrument_codeflash_capture(
839836
self.function_to_optimize, file_path_to_helper_classes, self.test_cfg.tests_root
@@ -850,15 +847,18 @@ def establish_original_code_baseline(
850847
finally:
851848
# Remove codeflash capture
852849
self.write_code_and_helpers(
853-
self.function_to_optimize_source_code, original_helper_code, self.function_to_optimize.file_path
850+
original_fto_code, original_helper_code, self.function_to_optimize.file_path
854851
)
855852
if not behavioral_results:
856853
logger.warning(
857854
f"Couldn't run any tests for original function {self.function_to_optimize.function_name}. SKIPPING OPTIMIZING THIS FUNCTION."
858855
)
859856
console.rule()
860857
return Failure("Failed to establish a baseline for the original code - bevhavioral tests failed.")
861-
858+
if not coverage_critic(
859+
coverage_results, self.args.test_framework
860+
):
861+
return Failure("The threshold for test coverage was not met.")
862862
if test_framework == "pytest":
863863
benchmarking_results, _ = self.run_and_parse_tests(
864864
testing_type=TestingMode.PERFORMANCE,

codeflash/optimization/optimizer.py

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ def run(self) -> None:
7171
console.rule()
7272
if not env_utils.ensure_codeflash_api_key():
7373
return
74-
74+
function_optimizer = None
7575
file_to_funcs_to_optimize: dict[Path, list[FunctionToOptimize]]
7676
num_optimizable_functions: int
7777
(file_to_funcs_to_optimize, num_optimizable_functions) = get_functions_to_optimize(
@@ -166,18 +166,7 @@ def run(self) -> None:
166166
function_optimizer = self.create_function_optimizer(
167167
function_to_optimize, function_to_optimize_ast, function_to_tests, validated_original_code[original_module_path].source_code
168168
)
169-
try:
170-
best_optimization = function_optimizer.optimize_function()
171-
finally:
172-
for test_file in function_optimizer.test_files.get_by_type(TestType.GENERATED_REGRESSION).test_files:
173-
test_file.instrumented_behavior_file_path.unlink(missing_ok=True)
174-
test_file.benchmarking_file_path.unlink(missing_ok=True)
175-
for test_file in function_optimizer.test_files.get_by_type(TestType.EXISTING_UNIT_TEST).test_files:
176-
test_file.instrumented_behavior_file_path.unlink(missing_ok=True)
177-
test_file.benchmarking_file_path.unlink(missing_ok=True)
178-
for test_file in function_optimizer.test_files.get_by_type(TestType.CONCOLIC_COVERAGE_TEST).test_files:
179-
test_file.instrumented_behavior_file_path.unlink(missing_ok=True)
180-
169+
best_optimization = function_optimizer.optimize_function()
181170
if is_successful(best_optimization):
182171
optimizations_found += 1
183172
else:
@@ -190,10 +179,20 @@ def run(self) -> None:
190179
elif self.args.all:
191180
logger.info("✨ All functions have been optimized! ✨")
192181
finally:
182+
if function_optimizer:
183+
for test_file in function_optimizer.test_files.get_by_type(TestType.GENERATED_REGRESSION).test_files:
184+
test_file.instrumented_behavior_file_path.unlink(missing_ok=True)
185+
test_file.benchmarking_file_path.unlink(missing_ok=True)
186+
for test_file in function_optimizer.test_files.get_by_type(TestType.EXISTING_UNIT_TEST).test_files:
187+
test_file.instrumented_behavior_file_path.unlink(missing_ok=True)
188+
test_file.benchmarking_file_path.unlink(missing_ok=True)
189+
for test_file in function_optimizer.test_files.get_by_type(TestType.CONCOLIC_COVERAGE_TEST).test_files:
190+
test_file.instrumented_behavior_file_path.unlink(missing_ok=True)
191+
if function_optimizer.test_cfg.concolic_test_root_dir:
192+
shutil.rmtree(function_optimizer.test_cfg.concolic_test_root_dir, ignore_errors=True)
193193
if hasattr(get_run_tmp_file, "tmpdir"):
194194
get_run_tmp_file.tmpdir.cleanup()
195-
if self.test_cfg.concolic_test_root_dir:
196-
shutil.rmtree(self.test_cfg.concolic_test_root_dir, ignore_errors=True)
195+
197196

198197

199198
def run_with_args(args: Namespace) -> None:

0 commit comments

Comments
 (0)