From 9903d86482bd51f2c8160a11e668c908bf4e6e96 Mon Sep 17 00:00:00 2001 From: "codeflash-ai[bot]" <148906541+codeflash-ai[bot]@users.noreply.github.com> Date: Thu, 12 Feb 2026 16:29:25 +0000 Subject: [PATCH 1/2] Optimize is_inside_string MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The optimized code achieves a **58% runtime improvement** (from 991μs to 627μs) by replacing character-by-character iteration with a **regex-based fast-path** that jumps directly to the next "special" character (quotes or backslashes). **Key optimization:** Instead of examining every character in the code string with Python-level operations (`code[i]`, `char in "\"'`"`), the optimized version uses a precompiled regex pattern (`_SPECIAL_RE`) to scan for the next relevant character in C code (via the regex engine). This dramatically reduces Python interpreter overhead. **Why this works:** - The original code spent ~16% of time in the `while i < pos` loop condition checks and ~17.8% indexing into the string (`char = code[i]`) - The optimized code reduces loop iterations from 21,270 to just 1,859 by skipping over long stretches of non-special characters - For long strings without quotes/backslashes, `search()` can scan hundreds/thousands of characters in a single C-level operation instead of iterating in Python **Performance characteristics based on test results:** - **Small strings (< 20 chars)**: Actually 50-70% slower due to regex overhead - the setup cost of calling `search()` outweighs the benefit - **Large strings (> 1000 chars)**: Massive speedups of 500-1300% - the fast-path shines when scanning long runs of normal code - **Medium strings (20-1000 chars)**: Mixed results - slight slowdowns to moderate gains depending on quote density **Impact on workloads:** The function is called from test instrumentation code (as shown in `test_javascript_instrumentation.py`), where it checks if positions in JavaScript code are inside string literals. In real-world instrumentation scenarios with typical JavaScript files (hundreds to thousands of characters), this optimization will significantly reduce overhead when instrumenting or analyzing code, especially when checking many positions in files with long non-string sections. The trade-off of slower performance on very small strings is acceptable because the absolute time difference (nanoseconds vs microseconds) is negligible, while the gains on realistically-sized code files are substantial. --- codeflash/languages/javascript/instrument.py | 35 +++++++++++++++++--- 1 file changed, 30 insertions(+), 5 deletions(-) diff --git a/codeflash/languages/javascript/instrument.py b/codeflash/languages/javascript/instrument.py index dee534044..e9582f298 100644 --- a/codeflash/languages/javascript/instrument.py +++ b/codeflash/languages/javascript/instrument.py @@ -17,6 +17,8 @@ from codeflash.code_utils.code_position import CodePosition from codeflash.discovery.functions_to_optimize import FunctionToOptimize +_SPECIAL_RE = re.compile(r'["\'`\\]') + class TestingMode: """Testing mode constants.""" @@ -74,24 +76,47 @@ def is_inside_string(code: str, pos: int) -> bool: string_char = None i = 0 + + # Quick check to preserve original behavior that accessing beyond the end + # of code raises IndexError (original loop would raise when i == len(code)). + if pos > len(code): + raise IndexError("string index out of range") + + s = code + search = _SPECIAL_RE.search + n = len(s) + while i < pos: - char = code[i] + # Find next special character (quote or backslash) up to pos. + m = search(s, i, pos) + if not m: + # No special characters before pos; we're done. + break + + j = m.start() + char = s[j] + if in_string: # Check for escape sequence - if char == "\\" and i + 1 < len(code): - i += 2 # Skip escaped character + if char == "\\" and j + 1 < n: + # Skip escaped character + i = j + 2 continue # Check for end of string if char == string_char: in_string = False string_char = None + # Move past this special character + i = j + 1 + continue + # Check for start of string - elif char in "\"'`": + if char in "\"'`": in_string = True string_char = char - i += 1 + i = j + 1 return in_string From a0673d570685aa519118b349823ce4682ecec21e Mon Sep 17 00:00:00 2001 From: "claude[bot]" <41898282+claude[bot]@users.noreply.github.com> Date: Thu, 12 Feb 2026 16:31:33 +0000 Subject: [PATCH 2/2] style: auto-fix linting issues --- codeflash/languages/javascript/instrument.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/codeflash/languages/javascript/instrument.py b/codeflash/languages/javascript/instrument.py index e9582f298..da0e333f8 100644 --- a/codeflash/languages/javascript/instrument.py +++ b/codeflash/languages/javascript/instrument.py @@ -76,7 +76,6 @@ def is_inside_string(code: str, pos: int) -> bool: string_char = None i = 0 - # Quick check to preserve original behavior that accessing beyond the end # of code raises IndexError (original loop would raise when i == len(code)). if pos > len(code): @@ -96,7 +95,6 @@ def is_inside_string(code: str, pos: int) -> bool: j = m.start() char = s[j] - if in_string: # Check for escape sequence if char == "\\" and j + 1 < n: