From b269dcd31128ef9a048643281c208d0379a3e7bc Mon Sep 17 00:00:00 2001 From: "codeflash-ai[bot]" <148906541+codeflash-ai[bot]@users.noreply.github.com> Date: Sun, 6 Jul 2025 00:45:54 +0000 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=EF=B8=8F=20Speed=20up=20function=20`q?= =?UTF-8?q?uantity=5Fof=5Ftests=5Fcritic`=20by=2076%=20in=20PR=20#517=20(`?= =?UTF-8?q?early-skip-if-insufficient-number-of-tests-passed`)=20Here=20is?= =?UTF-8?q?=20the=20optimized=20code=20(with=20all=20comments=20preserved?= =?UTF-8?q?=20except=20for=20the=20lines=20that=20are=20modified).=20The?= =?UTF-8?q?=20key=20optimizations=20are.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- codeflash/result/critic.py | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/codeflash/result/critic.py b/codeflash/result/critic.py index 323482323..d863b11b8 100644 --- a/codeflash/result/critic.py +++ b/codeflash/result/critic.py @@ -9,7 +9,7 @@ MIN_IMPROVEMENT_THRESHOLD, MIN_TESTCASE_PASSED_THRESHOLD, ) -from codeflash.models.models import TestType +from codeflash.models.models import OptimizedCandidateResult, OriginalCodeBaseline, TestType if TYPE_CHECKING: from codeflash.models.models import CoverageData, OptimizedCandidateResult, OriginalCodeBaseline @@ -52,16 +52,27 @@ def speedup_critic( def quantity_of_tests_critic(candidate_result: OptimizedCandidateResult | OriginalCodeBaseline) -> bool: test_results = candidate_result.behavior_test_results - report = test_results.get_test_pass_fail_report_by_type() - + # Fast path: compute report, pass_count and REPLAY_TEST in a single loop + report = {} pass_count = 0 - for test_type in report: - pass_count += report[test_type]["passed"] + replay_pass = 0 + for test_result in test_results.test_results: + if test_result.loop_index == 1: + ttype = test_result.test_type + if ttype not in report: + report[ttype] = {"passed": 0, "failed": 0} + if test_result.did_pass: + report[ttype]["passed"] += 1 + pass_count += 1 + if ttype == TestType.REPLAY_TEST: + replay_pass += 1 + else: + report[ttype]["failed"] += 1 if pass_count >= MIN_TESTCASE_PASSED_THRESHOLD: return True - # If one or more tests passed, check if least one of them was a successful REPLAY_TEST - return bool(pass_count >= 1 and report[TestType.REPLAY_TEST]["passed"] >= 1) + # If at least one test passed, check if at least one was a successful REPLAY_TEST + return bool(pass_count >= 1 and replay_pass >= 1) def coverage_critic(original_code_coverage: CoverageData | None, test_framework: str) -> bool: