Skip to content

Commit 50f4c33

Browse files
committed
10% chance of optimizing again
1 parent 4f39794 commit 50f4c33

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

codeflash/optimization/function_optimizer.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import ast
44
import concurrent.futures
55
import os
6+
import random
67
import subprocess
78
import time
89
import uuid
@@ -40,6 +41,7 @@
4041
INDIVIDUAL_TESTCASE_TIMEOUT,
4142
N_CANDIDATES,
4243
N_TESTS_TO_GENERATE,
44+
REPEAT_OPTIMIZATION_PROBABILITY,
4345
TOTAL_LOOPING_TIME,
4446
)
4547
from codeflash.code_utils.edit_generated_tests import (
@@ -155,7 +157,13 @@ def optimize_function(self) -> Result[BestOptimization, str]: # noqa: PLR0911
155157

156158
if has_any_async_functions(code_context.read_writable_code):
157159
return Failure("Codeflash does not support async functions in the code to optimize.")
158-
if check_optimization_status(self.function_to_optimize, code_context):
160+
# Random here means that we still attempt optimization with a fractional chance to see if
161+
# last time we could not find an optimization, maybe this time we do.
162+
# Random is before as a performance optimization, swapping the two 'and' statements has the same effect
163+
if (
164+
random.random() > REPEAT_OPTIMIZATION_PROBABILITY # noqa: S311
165+
and check_optimization_status(self.function_to_optimize, code_context)
166+
):
159167
return Failure("This function has previously been optimized, skipping.")
160168

161169
code_print(code_context.read_writable_code)

0 commit comments

Comments
 (0)