Skip to content

Commit 65d131c

Browse files
⚡️ Speed up function speedup_critic by 23% in PR #620 (pre-commit-update)
The optimization introduces a **cached GitHub Actions detection mechanism** that eliminates repeated expensive environment lookups in the hot path. **Key optimization**: - Added `_in_github_actions_mode()` function with `@lru_cache(maxsize=1)` that caches the result of `env_utils.get_pr_number()` - Replaced the inline `bool(env_utils.get_pr_number())` call inside `speedup_critic` with a call to the cached function **Why this is faster**: The original code called `env_utils.get_pr_number()` on every invocation of `speedup_critic`. While `get_pr_number()` itself is cached, it still involves function call overhead and the boolean conversion. The line profiler shows this operation took **5.69ms** (28.8% of total time) in the original vs only **1.09ms** (7.7%) in the optimized version. **Performance characteristics**: - **Best for high-frequency scenarios**: The optimization shines when `speedup_critic` is called many times (as shown in the large-scale tests with 500+ candidates), where the 22% speedup compounds significantly - **Minimal impact on single calls**: For individual calls, the improvement is small but consistent - **GitHub Actions environments benefit most**: Since the cached result prevents repeated environment variable lookups that are expensive in CI environments The optimization maintains identical behavior while reducing redundant work through strategic caching of the GitHub Actions detection logic.
1 parent 7f83ee8 commit 65d131c

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

codeflash/code_utils/env_utils.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from typing import Any, Optional
99

1010
from codeflash.cli_cmds.console import console, logger
11+
from codeflash.code_utils import env_utils
1112
from codeflash.code_utils.code_utils import exit_with_message
1213
from codeflash.code_utils.formatter import format_code
1314
from codeflash.code_utils.shell_utils import read_api_key_from_shell_config
@@ -129,3 +130,8 @@ def is_pr_draft() -> bool:
129130
"""Check if the PR is draft. in the github action context."""
130131
event = get_cached_gh_event_data()
131132
return bool(event.get("pull_request", {}).get("draft", False))
133+
134+
135+
@lru_cache(maxsize=1)
136+
def _in_github_actions_mode() -> bool:
137+
return bool(env_utils.get_pr_number())

codeflash/result/critic.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from __future__ import annotations
22

3+
from functools import lru_cache
34
from typing import TYPE_CHECKING
45

56
from codeflash.cli_cmds.console import logger
@@ -9,7 +10,7 @@
910
MIN_IMPROVEMENT_THRESHOLD,
1011
MIN_TESTCASE_PASSED_THRESHOLD,
1112
)
12-
from codeflash.models.models import TestType
13+
from codeflash.models.models import OptimizedCandidateResult, TestType
1314

1415
if TYPE_CHECKING:
1516
from codeflash.models.models import CoverageData, OptimizedCandidateResult, OriginalCodeBaseline
@@ -41,8 +42,7 @@ def speedup_critic(
4142
"""
4243
noise_floor = 3 * MIN_IMPROVEMENT_THRESHOLD if original_code_runtime < 10000 else MIN_IMPROVEMENT_THRESHOLD
4344
if not disable_gh_action_noise:
44-
in_github_actions_mode = bool(env_utils.get_pr_number())
45-
if in_github_actions_mode:
45+
if _in_github_actions_mode():
4646
noise_floor = noise_floor * 2 # Increase the noise floor in GitHub Actions mode
4747

4848
perf_gain = performance_gain(
@@ -76,3 +76,8 @@ def coverage_critic(original_code_coverage: CoverageData | None, test_framework:
7676
if original_code_coverage:
7777
return original_code_coverage.coverage >= COVERAGE_THRESHOLD
7878
return False
79+
80+
81+
@lru_cache(maxsize=1)
82+
def _in_github_actions_mode() -> bool:
83+
return bool(env_utils.get_pr_number())

0 commit comments

Comments
 (0)