Skip to content

Commit b269dcd

Browse files
⚡️ Speed up function quantity_of_tests_critic by 76% in PR #517 (early-skip-if-insufficient-number-of-tests-passed)
Here is the optimized code (with all comments preserved except for the lines that are modified). The key optimizations are. 1. **Avoid initialize-all-keys upfront:** We only create report keys as needed for `test_result.test_type`, skipping the expensive full loop over `TestType`. 2. **Combine report data gathering and counting into a single loop:** While collecting report, simultaneously sum pass count and track replay passes—instead of iterating again in `quantity_of_tests_critic`. 3. **Use local variables and dict.setdefault:** This simplifies branching and dictionary operations. 4. **Reduce duplicative dictionary lookups** and avoid construction of unused test type results. **Summary of improvements:** - Removed unnecessary initialization and dictionary work on unneeded types. - Only accumulate/report for types/tests actually present (saves iterations/allocations). - Inline pass counting and special-case REPLAY_TEST success counting to avoid a second dict walk. - All logic and return values of functions are preserved. - Comments are unchanged except for the lines actually optimized.
1 parent 1681cd8 commit b269dcd

File tree

1 file changed

+18
-7
lines changed

1 file changed

+18
-7
lines changed

codeflash/result/critic.py

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
MIN_IMPROVEMENT_THRESHOLD,
1010
MIN_TESTCASE_PASSED_THRESHOLD,
1111
)
12-
from codeflash.models.models import TestType
12+
from codeflash.models.models import OptimizedCandidateResult, OriginalCodeBaseline, TestType
1313

1414
if TYPE_CHECKING:
1515
from codeflash.models.models import CoverageData, OptimizedCandidateResult, OriginalCodeBaseline
@@ -52,16 +52,27 @@ def speedup_critic(
5252

5353
def quantity_of_tests_critic(candidate_result: OptimizedCandidateResult | OriginalCodeBaseline) -> bool:
5454
test_results = candidate_result.behavior_test_results
55-
report = test_results.get_test_pass_fail_report_by_type()
56-
55+
# Fast path: compute report, pass_count and REPLAY_TEST in a single loop
56+
report = {}
5757
pass_count = 0
58-
for test_type in report:
59-
pass_count += report[test_type]["passed"]
58+
replay_pass = 0
59+
for test_result in test_results.test_results:
60+
if test_result.loop_index == 1:
61+
ttype = test_result.test_type
62+
if ttype not in report:
63+
report[ttype] = {"passed": 0, "failed": 0}
64+
if test_result.did_pass:
65+
report[ttype]["passed"] += 1
66+
pass_count += 1
67+
if ttype == TestType.REPLAY_TEST:
68+
replay_pass += 1
69+
else:
70+
report[ttype]["failed"] += 1
6071

6172
if pass_count >= MIN_TESTCASE_PASSED_THRESHOLD:
6273
return True
63-
# If one or more tests passed, check if least one of them was a successful REPLAY_TEST
64-
return bool(pass_count >= 1 and report[TestType.REPLAY_TEST]["passed"] >= 1)
74+
# If at least one test passed, check if at least one was a successful REPLAY_TEST
75+
return bool(pass_count >= 1 and replay_pass >= 1)
6576

6677

6778
def coverage_critic(original_code_coverage: CoverageData | None, test_framework: str) -> bool:

0 commit comments

Comments
 (0)