From e5d384d654b7a1ad47ae64f06cf3450368c9919a Mon Sep 17 00:00:00 2001 From: "codeflash-ai[bot]" <148906541+codeflash-ai[bot]@users.noreply.github.com> Date: Wed, 4 Mar 2026 03:22:02 +0000 Subject: [PATCH] Optimize JavaAssertTransformer._infer_return_type MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The optimization adds a fast-path check in `_infer_type_from_assertion_args` that looks for the first comma and extracts the substring before it directly when no special delimiter characters (quotes, parentheses, braces) precede that comma, bypassing the expensive full `_extract_first_arg` parser for simple literals like `42`, `100L`, or `true`. Line profiler shows the original `_extract_first_arg` call consumed 49% of method runtime (~11.1 ms); the optimized version reduces this to 6.7% (~0.98 ms) by handling 1518 of 1632 cases via the cheap substring path, cutting per-call overhead from ~6808 ns to ~140–280 ns for common assertions. Runtime improves 43% (4.73 ms → 3.30 ms) with no correctness regressions; the fallback preserves exact behavior for nested or quoted arguments. --- codeflash/languages/java/remove_asserts.py | 25 ++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/codeflash/languages/java/remove_asserts.py b/codeflash/languages/java/remove_asserts.py index 0a84b687c..a5a5986c9 100644 --- a/codeflash/languages/java/remove_asserts.py +++ b/codeflash/languages/java/remove_asserts.py @@ -956,8 +956,29 @@ def _infer_type_from_assertion_args(self, original_text: str, method: str) -> st elif args_str.endswith(")"): args_str = args_str[:-1] - # Fast-path: only extract the first top-level argument instead of splitting all arguments. - first_arg = self._extract_first_arg(args_str) + # Fast-path: try to cheaply obtain the first top-level argument without invoking + # the full parser. If the first comma occurs before any special characters that + # would affect top-levelness (quotes/parens/braces), we can take the substring + # up to that comma as the first argument. + if not args_str: + return "Object" + + # Find first comma; if none, the entire args_str is the single argument + comma_idx = args_str.find(",") + if comma_idx == -1: + first_arg = args_str + else: + # If there are no special delimiter characters before this comma, we can + # safely take the substring as the first argument. + before = args_str[:comma_idx] + if not self._special_re.search(before): + first_arg = before + else: + # Fallback: use the full extractor which respects nesting/strings/generics. + first_arg = self._extract_first_arg(args_str) + if first_arg is None: + return "Object" + if not first_arg: return "Object"