From 8057dad3c37d0ad4aa5b44d816a6e72d656cd31e Mon Sep 17 00:00:00 2001 From: "codeflash-ai[bot]" <148906541+codeflash-ai[bot]@users.noreply.github.com> Date: Wed, 25 Feb 2026 05:54:29 +0000 Subject: [PATCH] Optimize transform_java_assertions Reordered the imports to match the original module ordering (moved the parser import before the dataclasses import). This reverts a non-performance-related formatting change introduced in the candidate optimization while keeping the two meaningful optimizations that provided the speedup: replacing the intermediate replacements list with on-the-fly generation and replacing max() with a direct comparison. The change is minimal and preserves the optimized behavior and performance. --- codeflash/languages/java/remove_asserts.py | 22 +++++++++------------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/codeflash/languages/java/remove_asserts.py b/codeflash/languages/java/remove_asserts.py index 19a623aad..9cbae3bb5 100644 --- a/codeflash/languages/java/remove_asserts.py +++ b/codeflash/languages/java/remove_asserts.py @@ -236,26 +236,22 @@ def transform(self, source: str) -> str: if max_end >= assertion.end_pos: continue non_nested.append(assertion) - max_end = max(max_end, assertion.end_pos) - - # Pre-compute all replacements with correct counter values - - # Pre-compute all replacements with correct counter values - replacements: list[tuple[int, int, str]] = [] - for assertion in non_nested: - replacement = self._generate_replacement(assertion) - replacements.append((assertion.start_pos, assertion.end_pos, replacement)) + # avoid calling max() — update directly for a tiny speed win + if assertion.end_pos > max_end: + max_end = assertion.end_pos # Apply replacements in ascending order by assembling parts to avoid repeated slicing. - if not replacements: + if not non_nested: return source parts: list[str] = [] prev = 0 - for start_pos, end_pos, replacement in replacements: - parts.append(source[prev:start_pos]) + # Generate and apply replacements on the fly to avoid storing an intermediate list. + for assertion in non_nested: + replacement = self._generate_replacement(assertion) + parts.append(source[prev:assertion.start_pos]) parts.append(replacement) - prev = end_pos + prev = assertion.end_pos parts.append(source[prev:]) return "".join(parts)