Skip to content

Commit 957eb97

Browse files
saga4saga4
authored andcommitted
fix inline condition
1 parent 520d87c commit 957eb97

File tree

4 files changed

+33
-39
lines changed

4 files changed

+33
-39
lines changed

codeflash/api/aiservice.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from pydantic.json import pydantic_encoder
1111

1212
from codeflash.cli_cmds.console import console, logger
13-
from codeflash.code_utils.config_consts import get_n_candidates, get_n_candidates_lp
13+
from codeflash.code_utils.config_consts import N_CANDIDATES_EFFECTIVE, N_CANDIDATES_LP_EFFECTIVE
1414
from codeflash.code_utils.env_utils import get_codeflash_api_key
1515
from codeflash.code_utils.git_utils import get_last_commit_author_if_pr_exists, get_repo_owner_and_name
1616
from codeflash.lsp.helpers import is_LSP_enabled
@@ -132,7 +132,7 @@ def optimize_python_code( # noqa: D417
132132
"current_username": get_last_commit_author_if_pr_exists(None),
133133
"repo_owner": git_repo_owner,
134134
"repo_name": git_repo_name,
135-
"n_candidates": get_n_candidates(),
135+
"n_candidates": N_CANDIDATES_EFFECTIVE,
136136
}
137137

138138
logger.info("Generating optimized candidates…")
@@ -194,7 +194,7 @@ def optimize_python_code_line_profiler( # noqa: D417
194194
"experiment_metadata": experiment_metadata,
195195
"codeflash_version": codeflash_version,
196196
"lsp_mode": is_LSP_enabled(),
197-
"n_candidates_lp": get_n_candidates_lp(),
197+
"n_candidates_lp": N_CANDIDATES_LP_EFFECTIVE,
198198
}
199199

200200
logger.info("Generating optimized candidates…")

codeflash/code_utils/config_consts.py

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -19,26 +19,20 @@
1919
TOTAL_LOOPING_TIME_LSP = 10.0 # Kept same timing for LSP mode to avoid in increase in performance reporting
2020
N_CANDIDATES_LP_LSP = 3
2121

22+
# Max limits to prevent excessive API calls
23+
MAX_N_CANDIDATES = 5
24+
MAX_N_CANDIDATES_LP = 6
2225

23-
def get_n_candidates() -> int:
26+
# Inlined variables - determine LSP mode once at import time
27+
try:
2428
from codeflash.lsp.helpers import is_LSP_enabled
2529

26-
return N_CANDIDATES_LSP if is_LSP_enabled() else N_CANDIDATES
30+
_IS_LSP_ENABLED = is_LSP_enabled()
31+
except ImportError:
32+
_IS_LSP_ENABLED = False
2733

28-
29-
def get_n_candidates_lp() -> int:
30-
from codeflash.lsp.helpers import is_LSP_enabled
31-
32-
return N_CANDIDATES_LP_LSP if is_LSP_enabled() else N_CANDIDATES_LP
33-
34-
35-
def get_n_tests_to_generate() -> int:
36-
from codeflash.lsp.helpers import is_LSP_enabled
37-
38-
return N_TESTS_TO_GENERATE_LSP if is_LSP_enabled() else N_TESTS_TO_GENERATE
39-
40-
41-
def get_total_looping_time() -> float:
42-
from codeflash.lsp.helpers import is_LSP_enabled
43-
44-
return TOTAL_LOOPING_TIME_LSP if is_LSP_enabled() else TOTAL_LOOPING_TIME
34+
# Direct variables with max limits applied
35+
N_CANDIDATES_EFFECTIVE = min(N_CANDIDATES_LSP if _IS_LSP_ENABLED else N_CANDIDATES, MAX_N_CANDIDATES)
36+
N_CANDIDATES_LP_EFFECTIVE = min(N_CANDIDATES_LP_LSP if _IS_LSP_ENABLED else N_CANDIDATES_LP, MAX_N_CANDIDATES_LP)
37+
N_TESTS_TO_GENERATE_EFFECTIVE = N_TESTS_TO_GENERATE_LSP if _IS_LSP_ENABLED else N_TESTS_TO_GENERATE
38+
TOTAL_LOOPING_TIME_EFFECTIVE = TOTAL_LOOPING_TIME_LSP if _IS_LSP_ENABLED else TOTAL_LOOPING_TIME

codeflash/code_utils/git_utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
from unidiff import PatchSet
1717

1818
from codeflash.cli_cmds.console import logger
19-
from codeflash.code_utils.config_consts import get_n_candidates
19+
from codeflash.code_utils.config_consts import N_CANDIDATES_EFFECTIVE
2020

2121
if TYPE_CHECKING:
2222
from git import Repo
@@ -164,7 +164,7 @@ def create_git_worktrees(
164164
) -> tuple[Path | None, list[Path]]:
165165
if git_root and worktree_root_dir:
166166
worktree_root = Path(tempfile.mkdtemp(dir=worktree_root_dir))
167-
worktrees = [Path(tempfile.mkdtemp(dir=worktree_root)) for _ in range(get_n_candidates() + 1)]
167+
worktrees = [Path(tempfile.mkdtemp(dir=worktree_root)) for _ in range(N_CANDIDATES_EFFECTIVE + 1)]
168168
for worktree in worktrees:
169169
subprocess.run(["git", "worktree", "add", "-d", worktree], cwd=module_root, check=True)
170170
else:

codeflash/optimization/function_optimizer.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,11 @@
4343
)
4444
from codeflash.code_utils.config_consts import (
4545
INDIVIDUAL_TESTCASE_TIMEOUT,
46+
N_CANDIDATES_EFFECTIVE,
47+
N_CANDIDATES_LP_EFFECTIVE,
48+
N_TESTS_TO_GENERATE_EFFECTIVE,
4649
REPEAT_OPTIMIZATION_PROBABILITY,
47-
get_n_candidates,
48-
get_n_candidates_lp,
49-
get_n_tests_to_generate,
50-
get_total_looping_time,
50+
TOTAL_LOOPING_TIME_EFFECTIVE,
5151
)
5252
from codeflash.code_utils.deduplicate_code import normalize_code
5353
from codeflash.code_utils.edit_generated_tests import (
@@ -232,7 +232,7 @@ def __init__(
232232
self.generate_and_instrument_tests_results: (
233233
tuple[GeneratedTestsList, dict[str, set[FunctionCalledInTest]], OptimizationSet] | None
234234
) = None
235-
n_tests = get_n_tests_to_generate()
235+
n_tests = N_TESTS_TO_GENERATE_EFFECTIVE
236236
self.executor = concurrent.futures.ThreadPoolExecutor(
237237
max_workers=n_tests + 2 if self.experiment_id is None else n_tests + 3
238238
)
@@ -284,7 +284,7 @@ def generate_and_instrument_tests(
284284
]
285285
]:
286286
"""Generate and instrument tests, returning all necessary data for optimization."""
287-
n_tests = get_n_tests_to_generate()
287+
n_tests = N_TESTS_TO_GENERATE_EFFECTIVE
288288
generated_test_paths = [
289289
get_test_file_path(
290290
self.test_cfg.tests_root, self.function_to_optimize.function_name, test_index, test_type="unit"
@@ -477,7 +477,7 @@ def determine_best_candidate(
477477
dependency_code=code_context.read_only_context_code,
478478
trace_id=self.function_trace_id[:-4] + exp_type if self.experiment_id else self.function_trace_id,
479479
line_profiler_results=original_code_baseline.line_profile_results["str_out"],
480-
num_candidates=get_n_candidates_lp(),
480+
num_candidates=N_CANDIDATES_LP_EFFECTIVE,
481481
experiment_metadata=ExperimentMetadata(
482482
id=self.experiment_id, group="control" if exp_type == "EXP0" else "experiment"
483483
)
@@ -1013,7 +1013,7 @@ def generate_tests_and_optimizations(
10131013
generated_perf_test_paths: list[Path],
10141014
run_experiment: bool = False, # noqa: FBT001, FBT002
10151015
) -> Result[tuple[GeneratedTestsList, dict[str, set[FunctionCalledInTest]], OptimizationSet], str]:
1016-
n_tests = get_n_tests_to_generate()
1016+
n_tests = N_TESTS_TO_GENERATE_EFFECTIVE
10171017
assert len(generated_test_paths) == n_tests
10181018
console.rule()
10191019
# Submit the test generation task as future
@@ -1024,7 +1024,7 @@ def generate_tests_and_optimizations(
10241024
generated_test_paths,
10251025
generated_perf_test_paths,
10261026
)
1027-
n_candidates = get_n_candidates()
1027+
n_candidates = N_CANDIDATES_EFFECTIVE
10281028
future_optimization_candidates = self.executor.submit(
10291029
self.aiservice_client.optimize_python_code,
10301030
read_writable_code.markdown,
@@ -1421,7 +1421,7 @@ def establish_original_code_baseline(
14211421
instrument_codeflash_capture(
14221422
self.function_to_optimize, file_path_to_helper_classes, self.test_cfg.tests_root
14231423
)
1424-
total_looping_time = get_total_looping_time()
1424+
total_looping_time = TOTAL_LOOPING_TIME_EFFECTIVE
14251425
behavioral_results, coverage_results = self.run_and_parse_tests(
14261426
testing_type=TestingMode.BEHAVIOR,
14271427
test_env=test_env,
@@ -1561,7 +1561,7 @@ def run_optimized_candidate(
15611561
self.function_to_optimize, file_path_to_helper_classes, self.test_cfg.tests_root
15621562
)
15631563

1564-
total_looping_time = get_total_looping_time()
1564+
total_looping_time = TOTAL_LOOPING_TIME_EFFECTIVE
15651565
candidate_behavior_results, _ = self.run_and_parse_tests(
15661566
testing_type=TestingMode.BEHAVIOR,
15671567
test_env=test_env,
@@ -1614,7 +1614,7 @@ def run_optimized_candidate(
16141614
start_time: float = time.time()
16151615
loop_count = 0
16161616
for i in range(100):
1617-
if i >= 5 and time.time() - start_time >= get_total_looping_time() * 1.5:
1617+
if i >= 5 and time.time() - start_time >= TOTAL_LOOPING_TIME_EFFECTIVE * 1.5:
16181618
# * 1.5 to give unittest a bit more time to run
16191619
break
16201620
test_env["CODEFLASH_LOOP_INDEX"] = str(i + 1)
@@ -1623,7 +1623,7 @@ def run_optimized_candidate(
16231623
test_env=test_env,
16241624
test_files=self.test_files,
16251625
optimization_iteration=optimization_candidate_index,
1626-
testing_time=get_total_looping_time(),
1626+
testing_time=TOTAL_LOOPING_TIME_EFFECTIVE,
16271627
unittest_loop_index=i + 1,
16281628
)
16291629
loop_count = i + 1
@@ -1662,7 +1662,7 @@ def run_and_parse_tests(
16621662
test_env: dict[str, str],
16631663
test_files: TestFiles,
16641664
optimization_iteration: int,
1665-
testing_time: float = get_total_looping_time(),
1665+
testing_time: float = TOTAL_LOOPING_TIME_EFFECTIVE,
16661666
*,
16671667
enable_coverage: bool = False,
16681668
pytest_min_loops: int = 5,
@@ -1813,7 +1813,7 @@ def line_profiler_step(
18131813
test_env=test_env,
18141814
test_files=self.test_files,
18151815
optimization_iteration=0,
1816-
testing_time=get_total_looping_time(),
1816+
testing_time=TOTAL_LOOPING_TIME_EFFECTIVE,
18171817
enable_coverage=False,
18181818
code_context=code_context,
18191819
line_profiler_output_file=line_profiler_output_file,

0 commit comments

Comments
 (0)