From e74515f2778c20ac18a531facc7c874f6ac97880 Mon Sep 17 00:00:00 2001 From: "codeflash-ai[bot]" <148906541+codeflash-ai[bot]@users.noreply.github.com> Date: Fri, 3 Apr 2026 13:47:43 +0000 Subject: [PATCH] Optimize JavaAssertTransformer._generate_replacement MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The optimization added a dictionary cache (`_type_infer_cache`) to memoize the results of `_infer_type_from_assertion_args`, which is an expensive method involving regex operations and string parsing. Before, every call to `_infer_return_type` for a value assertion (e.g., `assertEquals`) would re-parse the assertion's original text, spending ~92% of the method's runtime in `_infer_type_from_assertion_args`. With caching keyed on `(original_text, method)`, repeated assertions with identical text reuse the inferred type, cutting `_infer_return_type` time by 59% (1.38 ms → 570 µs) and overall runtime by 20% (886 µs → 708 µs). No functional regressions observed across all test cases. --- codeflash/languages/java/remove_asserts.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/codeflash/languages/java/remove_asserts.py b/codeflash/languages/java/remove_asserts.py index 3289a9568..34908d89d 100644 --- a/codeflash/languages/java/remove_asserts.py +++ b/codeflash/languages/java/remove_asserts.py @@ -213,6 +213,9 @@ def __init__( self._CHAR_LITERAL_RE = re.compile(r"^'.'$|^'\\.'$") self._cast_re = re.compile(r"^\((\w+)\)") + # Cache for inferred types to avoid repeated expensive inference work + self._type_infer_cache: dict[tuple[str, str], str] = {} + def transform(self, source: str) -> str: """Remove assertions from source code, preserving target function calls. @@ -928,7 +931,13 @@ def _infer_return_type(self, assertion: AssertionMatch) -> str: # For assertEquals/assertNotEquals/assertSame, try to infer from the expected literal if method in JUNIT5_VALUE_ASSERTIONS: - return self._infer_type_from_assertion_args(assertion.original_text, method) + key = (assertion.original_text, method) + cached = self._type_infer_cache.get(key) + if cached is not None: + return cached + inferred = self._infer_type_from_assertion_args(assertion.original_text, method) + self._type_infer_cache[key] = inferred + return inferred # For fluent assertions (assertThat), type inference is harder — keep Object return "Object"