From a39edd21ec784fceae6569e5c85ef82b2d8c3e77 Mon Sep 17 00:00:00 2001 From: "codeflash-ai[bot]" <148906541+codeflash-ai[bot]@users.noreply.github.com> Date: Tue, 25 Feb 2025 23:29:03 +0000 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=EF=B8=8F=20Speed=20up=20method=20`Ass?= =?UTF-8?q?ertCleanup.=5Ftransform=5Fassert=5Fline`=20by=2034%=20in=20PR?= =?UTF-8?q?=20#26=20(`clean=5Fconcolic=5Ftests`)=20To=20optimize=20the=20`?= =?UTF-8?q?AssertCleanup`=20class,=20we=20can=20improve=20the=20`=5Ftransf?= =?UTF-8?q?orm=5Fassert=5Fline`=20method=20by=20reducing=20the=20use=20of?= =?UTF-8?q?=20regular=20expressions,=20and=20replacing=20them=20with=20mor?= =?UTF-8?q?e=20efficient=20string=20operations=20where=20possible.=20Where?= =?UTF-8?q?=20regular=20expressions=20are=20still=20necessary,=20we=20comp?= =?UTF-8?q?ile=20them=20once=20and=20reuse=20them.=20Here's=20the=20refact?= =?UTF-8?q?ored=20code.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ### Explanation of Changes. 1. **Regex Compilation in `__init__`**: Compiled the regular expressions in the `__init__` method to avoid recompiling them every time `_transform_assert_line` is called, improving speed. 2. **String Operations for Trailing Characters**: Replaced `re.sub` used to strip trailing commas or semicolons with simpler string operations, improving efficiency. These improvements help in optimizing the running speed of the program while maintaining the same functionality. --- codeflash/code_utils/code_replacer.py | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/codeflash/code_utils/code_replacer.py b/codeflash/code_utils/code_replacer.py index 269dd4706..28e252c47 100644 --- a/codeflash/code_utils/code_replacer.py +++ b/codeflash/code_utils/code_replacer.py @@ -354,21 +354,23 @@ def transform_asserts(self, code: str) -> str: return "\n".join(result_lines) def _transform_assert_line(self, line: str) -> Optional[str]: - indent = line[: len(line) - len(line.lstrip())] + indent_len = len(line) - len(line.lstrip()) + indent = line[:indent_len] - assert_match = re.match(r"\s*assert\s+(.*?)(?:\s*==\s*.*)?$", line) + assert_match = self.assert_pattern.match(line) if assert_match: expression = assert_match.group(1).strip() if expression.startswith("not "): return f"{indent}{expression}" - expression = re.sub(r"[,;]\s*$", "", expression) + # Removing trailing commas or semicolons without using regex + if expression and expression[-1] in ",;": + expression = expression[:-1] return f"{indent}{expression}" - unittest_match = re.match(r"(\s*)self\.assert([A-Za-z]+)\((.*)\)$", line) + unittest_match = self.unittest_pattern.match(line) if unittest_match: indent, assert_method, args = unittest_match.groups() - if args: arg_parts = self._split_top_level_args(args) if arg_parts and arg_parts[0]: @@ -399,6 +401,10 @@ def _split_top_level_args(self, args_str: str) -> list[str]: return result + def __init__(self): + self.assert_pattern = re.compile(r"\s*assert\s+(.*?)(?:\s*==\s*.*)?$") + self.unittest_pattern = re.compile(r"(\s*)self\.assert([A-Za-z]+)\((.*)\)$") + def clean_concolic_tests(test_suite_code: str) -> str: try: