From 1502b4d787db562cca8157ac1f66bbd0cbaa65db Mon Sep 17 00:00:00 2001 From: "codeflash-ai[bot]" <148906541+codeflash-ai[bot]@users.noreply.github.com> Date: Wed, 18 Feb 2026 13:11:33 +0000 Subject: [PATCH 1/2] Optimize StandaloneCallTransformer._parse_bracket_standalone_call The optimized code achieves a **147% speedup** (2.47x faster) by fundamentally changing how the `_find_balanced_parens` method searches through JavaScript code to find matching parentheses. **Key Optimization: Regex-Based Scanning** The original code iterates through **every single character** in the string (20,283 iterations) to find quotes and parentheses. The optimized version uses a precompiled regex pattern (`self._special_re = re.compile(r'["\'`()]')`) to **jump directly between special characters**, reducing iterations from 20,283 to just 1,268 - a **94% reduction in loop iterations**. **Why This Is Faster** 1. **Compiled regex scanning**: The regex engine (implemented in C) can skip over irrelevant characters much faster than Python's character-by-character iteration 2. **Fewer comparisons**: Instead of checking `if char in quotes` on every character, the regex only returns when it finds a relevant character 3. **Reduced overhead**: The optimized loop executes 16x fewer times, eliminating 19,000+ Python bytecode interpretation cycles **Line Profiler Evidence** The `_find_balanced_parens` function shows dramatic improvement: - **Original**: 18.4ms total (20,321 loop iterations @ 182.7ns per hit on the while condition) - **Optimized**: 3.2ms total (1,305 loop iterations @ 194.2ns per hit) - **5.7x faster** for this function alone The while loop body itself drops from consuming 98.3% of function time (checking every character) to the regex search consuming just 22.7% (jumping between special characters). **Trade-offs** The regex approach adds small overhead for each `match.group()` and `match.start()` call (9.3% + 7.5% of optimized time), but this is vastly outweighed by eliminating 19,000 character checks. **Impact** Since `_parse_bracket_standalone_call` spends 98.6% of its time in `_find_balanced_parens`, this optimization cascades up the call stack. This makes the code particularly beneficial when parsing JavaScript files with deeply nested function calls or large argument lists where the parenthesis-matching logic is heavily exercised. --- codeflash/languages/javascript/instrument.py | 21 +++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/codeflash/languages/javascript/instrument.py b/codeflash/languages/javascript/instrument.py index ee28c90a6..02ec2ae60 100644 --- a/codeflash/languages/javascript/instrument.py +++ b/codeflash/languages/javascript/instrument.py @@ -179,6 +179,9 @@ def __init__(self, function_to_optimize: FunctionToOptimize, capture_func: str) # Captures: (whitespace)(await )?(object.)*func_name.call( self._dot_call_pattern = re.compile(rf"(\s*)(await\s+)?((?:\w+\.)*){re.escape(self.func_name)}\.call\s*\(") + # Precompile regex to find next special character (quotes or parentheses) + self._special_re = re.compile(r'["\'`()]') + def transform(self, code: str) -> str: """Transform all standalone calls in the code.""" result: list[str] = [] @@ -380,13 +383,18 @@ def _find_balanced_parens(self, code: str, open_paren_pos: int) -> tuple[str | N s_len = len(s) quotes = "\"'`" + special_re = self._special_re + while pos < s_len and depth > 0: - char = s[pos] + match = special_re.search(s, pos) + if not match: + return None, -1 + + char = match.group() + char_pos = match.start() - # Handle string literals - # Note: preserve original escaping semantics (only checks immediate preceding char) if char in quotes: - prev_char = s[pos - 1] if pos > 0 else None + prev_char = s[char_pos - 1] if char_pos > 0 else None if prev_char != "\\": if not in_string: in_string = True @@ -399,13 +407,12 @@ def _find_balanced_parens(self, code: str, open_paren_pos: int) -> tuple[str | N depth += 1 elif char == ")": depth -= 1 - - pos += 1 + + pos = char_pos + 1 if depth != 0: return None, -1 - # slice once return s[open_paren_pos + 1 : pos - 1], pos def _parse_bracket_standalone_call(self, code: str, match: re.Match[str]) -> StandaloneCallMatch | None: From 801c709385f0c9b8526d8ce45fc7fe5ec96ef567 Mon Sep 17 00:00:00 2001 From: "claude[bot]" <41898282+claude[bot]@users.noreply.github.com> Date: Wed, 18 Feb 2026 13:13:57 +0000 Subject: [PATCH 2/2] style: fix trailing whitespace in instrument.py --- codeflash/languages/javascript/instrument.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/codeflash/languages/javascript/instrument.py b/codeflash/languages/javascript/instrument.py index 02ec2ae60..2ebfee96f 100644 --- a/codeflash/languages/javascript/instrument.py +++ b/codeflash/languages/javascript/instrument.py @@ -389,7 +389,7 @@ def _find_balanced_parens(self, code: str, open_paren_pos: int) -> tuple[str | N match = special_re.search(s, pos) if not match: return None, -1 - + char = match.group() char_pos = match.start() @@ -407,7 +407,7 @@ def _find_balanced_parens(self, code: str, open_paren_pos: int) -> tuple[str | N depth += 1 elif char == ")": depth -= 1 - + pos = char_pos + 1 if depth != 0: