Skip to content

Comments

⚡️ Speed up method ExpectCallTransformer.transform by 6,675% in PR #1318 (fix/js-jest30-loop-runner)#1469

Closed
codeflash-ai[bot] wants to merge 2 commits intomainfrom
codeflash/optimize-pr1318-2026-02-12T16.50.31
Closed

⚡️ Speed up method ExpectCallTransformer.transform by 6,675% in PR #1318 (fix/js-jest30-loop-runner)#1469
codeflash-ai[bot] wants to merge 2 commits intomainfrom
codeflash/optimize-pr1318-2026-02-12T16.50.31

Conversation

@codeflash-ai
Copy link
Contributor

@codeflash-ai codeflash-ai bot commented Feb 12, 2026

⚡️ This pull request contains optimizations for PR #1318

If you approve this dependent PR, these changes will be merged into the original PR branch fix/js-jest30-loop-runner.

This PR will be automatically closed if the original PR is merged.


📄 6,675% (66.75x) speedup for ExpectCallTransformer.transform in codeflash/languages/javascript/instrument.py

⏱️ Runtime : 789 milliseconds 11.6 milliseconds (best of 137 runs)

📝 Explanation and details

The optimized code achieves a 6,675% speedup (from 789ms to 11.6ms) by replacing an expensive O(n²) pattern with an O(n log n) approach for detecting whether regex matches occur inside string literals.

Original Performance Bottleneck:
The original code called is_inside_string(code, pos) for every regex match found. This function linearly scanned from position 0 to pos each time, checking character-by-character whether the position was inside a string literal. Line profiler shows is_inside_string consumed ~10 seconds (99.6% of total time in transform), with ~14.6 million character checks across all calls.

Key Optimization:
The optimized version introduces two new methods:

  1. _compute_string_spans(code) - Performs a single O(n) pass over the entire source code at the start of transform(), identifying all string literal regions and storing them as sorted (start, end) span lists
  2. _pos_inside_spans(pos, starts, ends) - Uses bisect.bisect_right() to perform O(log n) binary search lookups against the precomputed spans

Why This Works:

  • Eliminates redundant scanning: Instead of scanning the code prefix 1,641 times (once per match), we scan once and cache the results
  • Logarithmic lookups: Each string-check becomes O(log n) binary search instead of O(n) linear scan
  • Optimal for multiple matches: The more regex matches in the code, the greater the benefit. Test cases with 100-1000 expect calls show 1,000%+ speedups

Test Results Show Clear Pattern:

  • Small files (1-3 matches): 7-45% slower due to upfront span computation overhead
  • Medium files (50-100 matches): 565-1,151% faster
  • Large files (200-1000 matches): 1,836-11,489% faster

The optimization is particularly effective for the tool's primary use case: instrumenting JavaScript test files with many expect() calls, where the original quadratic behavior became prohibitively expensive.

Correctness verification report:

Test Status
⚙️ Existing Unit Tests 🔘 None Found
🌀 Generated Regression Tests 103 Passed
⏪ Replay Tests 🔘 None Found
🔎 Concolic Coverage Tests 🔘 None Found
📊 Tests Coverage 94.9%
🌀 Click to see Generated Regression Tests
from types import \
    SimpleNamespace  # used to create lightweight objects with attributes

# imports
import pytest  # used for our unit tests
from codeflash.languages.javascript.instrument import (ExpectCallTransformer,
                                                       is_inside_string)

# function to test

def make_transformer(function_name: str, qualified_name: str, capture_func: str = "capture", remove_assertions: bool = False):
    """
    Helper to create an ExpectCallTransformer instance.
    We use SimpleNamespace to mimic the minimal FunctionToOptimize object expected by the constructor:
    it only needs 'function_name' and 'qualified_name' attributes for the transformer to behave.
    """
    fn_obj = SimpleNamespace(function_name=function_name, qualified_name=qualified_name)
    return ExpectCallTransformer(fn_obj, capture_func, remove_assertions)

def test_is_inside_string_detects_quotes_and_escapes():
    # Basic sanity checks for the helper: positions inside and outside of string literals.
    text = 'console.log("hello \\"world\\"", expect(fn(1)).toBe(2));'
    # position of the 'e' in "expect" is inside the double-quoted string -> should be True
    pos = text.index("expect")
    # a position after the closing quote is not inside a string
    pos_after = text.index('));')  # a position after the quoted literal

def test_transform_basic_function_call_without_object_prefix():
    # Test a simple transformation of expect(fn(1)).toBe(2);
    transformer = make_transformer(function_name="fn", qualified_name="fn", capture_func="cap")
    src = "  expect(fn(1)).toBe(2);"
    # After transform, the expect should wrap a call to codeflash.cap with invocation id '1'
    expected = "  expect(codeflash.cap('fn', '1', fn, 1)).toBe(2);"
    codeflash_output = transformer.transform(src); out = codeflash_output # 12.9μs -> 13.5μs (4.75% slower)

def test_transform_object_method_binding_is_generated_correctly():
    # When the function is a method of an object, the transformer should bind the method
    transformer = make_transformer(function_name="fibonacci", qualified_name="calc.fibonacci", capture_func="cap")
    src = "expect(calc.fibonacci(5)).toBe(8);"
    expected = "expect(codeflash.cap('calc.fibonacci', '1', calc.fibonacci.bind(calc), 5)).toBe(8);"
    codeflash_output = transformer.transform(src); out = codeflash_output # 12.9μs -> 14.6μs (11.6% slower)

def test_transform_remove_assertions_removes_expect_and_assertion_and_preserves_args():
    # When remove_assertions=True, the wrapper expect/assertion should be removed and replaced with codeflash.cap call.
    transformer = make_transformer(function_name="fn", qualified_name="fn", capture_func="cap", remove_assertions=True)
    src = "    expect(fn('a', 2)).toBeTruthy();"
    # invocation id '1' should be used and the call should end with a semicolon
    expected = "    codeflash.cap('fn', '1', fn, 'a', 2);"
    codeflash_output = transformer.transform(src); out = codeflash_output # 13.2μs -> 16.0μs (17.3% slower)

def test_transform_no_args_function_call_with_remove_assertions():
    # When function has no args and remove_assertions=True, no extra comma should be present
    transformer = make_transformer(function_name="fn", qualified_name="fn", capture_func="cap", remove_assertions=True)
    src = "expect(fn()).toBeTruthy();"
    expected = "codeflash.cap('fn', '1', fn);"
    codeflash_output = transformer.transform(src); out = codeflash_output # 11.0μs -> 12.9μs (14.6% slower)

def test_transform_chain_with_resolves_and_not_and_multargs_assertion():
    # Complex assertion chains (negation + resolves + terminal assertion with multiple args)
    transformer = make_transformer(function_name="fn", qualified_name="fn", capture_func="cap")
    src = "expect(fn('x')).not.resolves.toBeCloseTo(0.5, 2);"
    # Ensure the assertion chain is preserved exactly and the wrapped call is injected
    expected = "expect(codeflash.cap('fn', '1', fn, 'x')).not.resolves.toBeCloseTo(0.5, 2);"
    codeflash_output = transformer.transform(src); out = codeflash_output # 15.7μs -> 19.2μs (18.4% slower)

def test_transform_skips_expect_inside_string_literal():
    # If an expect(...) occurs inside a string literal (e.g., a test description), it must not be transformed.
    transformer = make_transformer(function_name="fn", qualified_name="fn", capture_func="cap")
    src = 'it("description with expect(fn(1)).toBe(2) inside", () => {});'
    codeflash_output = transformer.transform(src); out = codeflash_output # 7.02μs -> 9.79μs (28.3% slower)

def test_transform_handles_unbalanced_function_parens_by_leaving_code_unchanged():
    # If the inner function call has unbalanced parens, the transformer should not crash and should leave the code intact.
    transformer = make_transformer(function_name="fn", qualified_name="fn", capture_func="cap")
    # Missing closing paren for fn(1
    src = "expect(fn(1).toBe(2);"
    codeflash_output = transformer.transform(src); out = codeflash_output # 6.97μs -> 7.88μs (11.5% slower)

def test_transform_rejects_assertion_chains_without_terminal_to_method():
    # A chain like .not without a final .toX should not be considered a valid expect assertion and thus not transformed.
    transformer = make_transformer(function_name="fn", qualified_name="fn", capture_func="cap")
    src = "expect(fn(1)).not;  // incomplete assertion"
    codeflash_output = transformer.transform(src); out = codeflash_output # 10.5μs -> 12.2μs (14.1% slower)

def test_transform_tobetruthy_no_args_assertion_is_supported():
    # Terminal assertion with no args (e.g., .toBeTruthy()) should be parsed and preserved
    transformer = make_transformer(function_name="fn", qualified_name="fn", capture_func="cap")
    src = "  expect(fn()).toBeTruthy();"
    expected = "  expect(codeflash.cap('fn', '1', fn)).toBeTruthy();"
    codeflash_output = transformer.transform(src); out = codeflash_output # 12.0μs -> 13.3μs (9.62% slower)

def test_transform_preserves_leading_whitespace_and_semicolon_absence():
    # If the original call does not have a trailing semicolon, the transformer should not insert one.
    transformer = make_transformer(function_name="fn", qualified_name="fn", capture_func="cap")
    src = "    expect(fn(1)).toBe(2)"  # no trailing semicolon
    expected = "    expect(codeflash.cap('fn', '1', fn, 1)).toBe(2)"  # no semicolon added
    codeflash_output = transformer.transform(src); out = codeflash_output # 11.6μs -> 12.7μs (9.29% slower)

def test_transform_many_calls_performance_and_invocation_ids():
    # Create 1000 simple expect calls and ensure each is transformed and receives sequential invocation ids.
    n = 1000
    transformer = make_transformer(function_name="fn", qualified_name="fn", capture_func="cap")
    # Build a large source with many expect calls, each on its own line
    lines = [f"expect(fn({i})).toBe({i*2});" for i in range(1, n + 1)]
    src = "\n".join(lines)
    codeflash_output = transformer.transform(src); out = codeflash_output # 724ms -> 6.25ms (11489% faster)
    # Check that the output contains the expected number of injected capture occurrences
    occurrences = out.count("codeflash.cap('fn',")
# codeflash_output is used to check that the output of the original code is the same as that of the optimized code.
import pytest
from codeflash.discovery.functions_to_optimize import FunctionToOptimize
from codeflash.languages.javascript.instrument import ExpectCallTransformer

# Helper function to create a FunctionToOptimize instance
def create_function_to_optimize(function_name: str, qualified_name: str) -> FunctionToOptimize:
    """Create a FunctionToOptimize instance for testing."""
    return FunctionToOptimize(
        function_name=function_name,
        qualified_name=qualified_name,
        file_path="test.js",
        line_number=1,
    )

class TestBasicTransform:
    """Test basic expect call transformations."""

    def test_simple_expect_call_with_single_arg(self):
        """Test transforming a simple expect call with a single argument."""
        func = create_function_to_optimize("add", "add")
        transformer = ExpectCallTransformer(func, "capture")
        code = "expect(add(1)).toBe(2);"
        codeflash_output = transformer.transform(code); result = codeflash_output # 13.8μs -> 14.0μs (1.58% slower)

    def test_simple_expect_call_with_multiple_args(self):
        """Test transforming an expect call with multiple arguments."""
        func = create_function_to_optimize("multiply", "multiply")
        transformer = ExpectCallTransformer(func, "capture")
        code = "expect(multiply(2, 3)).toBe(6);"
        codeflash_output = transformer.transform(code); result = codeflash_output # 13.4μs -> 14.2μs (6.25% slower)

    def test_expect_call_without_trailing_semicolon(self):
        """Test transforming an expect call without a trailing semicolon."""
        func = create_function_to_optimize("func", "func")
        transformer = ExpectCallTransformer(func, "capture")
        code = "expect(func(5)).toBe(10)"
        codeflash_output = transformer.transform(code); result = codeflash_output # 12.0μs -> 13.0μs (7.95% slower)

    def test_expect_call_with_tobetruthy_assertion(self):
        """Test transforming an expect call with toBeTruthy assertion."""
        func = create_function_to_optimize("isValid", "isValid")
        transformer = ExpectCallTransformer(func, "capture")
        code = "expect(isValid()).toBeTruthy();"
        codeflash_output = transformer.transform(code); result = codeflash_output # 12.2μs -> 13.3μs (7.75% slower)

    def test_expect_call_with_not_assertion(self):
        """Test transforming an expect call with .not chain."""
        func = create_function_to_optimize("isEmpty", "isEmpty")
        transformer = ExpectCallTransformer(func, "capture")
        code = "expect(isEmpty('text')).not.toBe(true);"
        codeflash_output = transformer.transform(code); result = codeflash_output # 15.0μs -> 17.6μs (14.3% slower)

    def test_expect_call_with_resolves_assertion(self):
        """Test transforming an expect call with .resolves chain."""
        func = create_function_to_optimize("fetchData", "fetchData")
        transformer = ExpectCallTransformer(func, "capture")
        code = "expect(fetchData()).resolves.toBe('data');"
        codeflash_output = transformer.transform(code); result = codeflash_output # 14.5μs -> 17.0μs (14.8% slower)

    def test_expect_call_with_leading_whitespace(self):
        """Test transforming an expect call with leading whitespace."""
        func = create_function_to_optimize("test", "test")
        transformer = ExpectCallTransformer(func, "capture")
        code = "    expect(test(1)).toBe(2);"
        codeflash_output = transformer.transform(code); result = codeflash_output # 12.0μs -> 13.1μs (8.54% slower)

    def test_invocation_counter_increments(self):
        """Test that invocation counter increments for each expect call."""
        func = create_function_to_optimize("func", "func")
        transformer = ExpectCallTransformer(func, "capture")
        code = "expect(func(1)).toBe(2);"
        transformer.transform(code) # 11.4μs -> 12.5μs (8.95% slower)

    def test_multiple_sequential_expect_calls(self):
        """Test transforming multiple sequential expect calls."""
        func = create_function_to_optimize("func", "func")
        transformer = ExpectCallTransformer(func, "capture")
        code = "expect(func(1)).toBe(2);\nexpect(func(3)).toBe(4);"
        codeflash_output = transformer.transform(code); result = codeflash_output # 19.2μs -> 19.6μs (1.92% slower)

    def test_remove_assertions_flag_true(self):
        """Test that remove_assertions=True removes the expect wrapper."""
        func = create_function_to_optimize("func", "func")
        transformer = ExpectCallTransformer(func, "capture", remove_assertions=True)
        code = "expect(func(1)).toBe(2);"
        codeflash_output = transformer.transform(code); result = codeflash_output # 11.2μs -> 12.1μs (7.38% slower)

    def test_remove_assertions_flag_false(self):
        """Test that remove_assertions=False keeps the expect wrapper."""
        func = create_function_to_optimize("func", "func")
        transformer = ExpectCallTransformer(func, "capture", remove_assertions=False)
        code = "expect(func(1)).toBe(2);"
        codeflash_output = transformer.transform(code); result = codeflash_output # 11.4μs -> 12.3μs (7.87% slower)

class TestEdgeCases:
    """Test edge cases and boundary conditions."""

    def test_empty_code_string(self):
        """Test transforming an empty code string."""
        func = create_function_to_optimize("func", "func")
        transformer = ExpectCallTransformer(func, "capture")
        codeflash_output = transformer.transform(""); result = codeflash_output # 818ns -> 1.12μs (27.2% slower)

    def test_code_with_no_expect_calls(self):
        """Test transforming code with no expect calls."""
        func = create_function_to_optimize("func", "func")
        transformer = ExpectCallTransformer(func, "capture")
        code = "const x = 5;\nconst y = func(x);"
        codeflash_output = transformer.transform(code); result = codeflash_output # 2.82μs -> 4.73μs (40.4% slower)

    def test_expect_call_in_string_literal_single_quotes(self):
        """Test that expect calls inside string literals are not transformed."""
        func = create_function_to_optimize("func", "func")
        transformer = ExpectCallTransformer(func, "capture")
        code = "const desc = 'expect(func(1)).toBe(2);';"
        codeflash_output = transformer.transform(code); result = codeflash_output # 6.39μs -> 8.67μs (26.3% slower)

    def test_expect_call_in_string_literal_double_quotes(self):
        """Test that expect calls in double-quoted strings are not transformed."""
        func = create_function_to_optimize("func", "func")
        transformer = ExpectCallTransformer(func, "capture")
        code = 'const desc = "expect(func(1)).toBe(2);";'
        codeflash_output = transformer.transform(code); result = codeflash_output # 6.02μs -> 8.18μs (26.4% slower)

    def test_expect_call_in_template_literal(self):
        """Test that expect calls in template literals are not transformed."""
        func = create_function_to_optimize("func", "func")
        transformer = ExpectCallTransformer(func, "capture")
        code = "const desc = `expect(func(1)).toBe(2);`;"
        codeflash_output = transformer.transform(code); result = codeflash_output # 6.01μs -> 8.16μs (26.3% slower)

    def test_function_name_with_special_characters_in_pattern(self):
        """Test transforming a function with special regex characters in name."""
        func = create_function_to_optimize("func$test", "func$test")
        transformer = ExpectCallTransformer(func, "capture")
        code = "expect(func$test(1)).toBe(2);"
        codeflash_output = transformer.transform(code); result = codeflash_output # 12.5μs -> 14.3μs (12.4% slower)

    def test_qualified_function_call_standalone(self):
        """Test that standalone function doesn't match method calls on objects."""
        func = create_function_to_optimize("add", "add")
        transformer = ExpectCallTransformer(func, "capture")
        code = "expect(calc.add(1)).toBe(2);"
        codeflash_output = transformer.transform(code); result = codeflash_output # 5.75μs -> 7.18μs (20.0% slower)

    def test_qualified_method_call_with_object_prefix(self):
        """Test that qualified method names match method calls on objects."""
        func = create_function_to_optimize("add", "calc.add")
        transformer = ExpectCallTransformer(func, "capture")
        code = "expect(calc.add(1)).toBe(2);"
        codeflash_output = transformer.transform(code); result = codeflash_output # 5.19μs -> 6.97μs (25.6% slower)

    def test_nested_function_calls_in_arguments(self):
        """Test expect calls with nested function calls in arguments."""
        func = create_function_to_optimize("outer", "outer")
        transformer = ExpectCallTransformer(func, "capture")
        code = "expect(outer(inner(1), another())).toBe(2);"
        codeflash_output = transformer.transform(code); result = codeflash_output # 14.7μs -> 16.4μs (10.4% slower)

    def test_unbalanced_parentheses_in_string_argument(self):
        """Test expect calls with string arguments containing parentheses."""
        func = create_function_to_optimize("format", "format")
        transformer = ExpectCallTransformer(func, "capture")
        code = "expect(format('test (with parens)')).toBe('output');"
        codeflash_output = transformer.transform(code); result = codeflash_output # 14.9μs -> 18.7μs (20.5% slower)

    def test_escaped_quotes_in_string_argument(self):
        """Test expect calls with escaped quotes in string arguments."""
        func = create_function_to_optimize("escape", "escape")
        transformer = ExpectCallTransformer(func, "capture")
        code = 'expect(escape("test\\"quote")).toBe("output");'
        codeflash_output = transformer.transform(code); result = codeflash_output # 14.2μs -> 17.8μs (19.9% slower)

    def test_assertion_with_multiple_arguments(self):
        """Test expect calls with assertions that take multiple arguments."""
        func = create_function_to_optimize("round", "round")
        transformer = ExpectCallTransformer(func, "capture")
        code = "expect(round(1.234)).toBeCloseTo(1.23, 2);"
        codeflash_output = transformer.transform(code); result = codeflash_output # 13.8μs -> 15.3μs (9.38% slower)

    def test_assertion_with_nested_function_call_argument(self):
        """Test assertion argument with nested function calls."""
        func = create_function_to_optimize("getValue", "getValue")
        transformer = ExpectCallTransformer(func, "capture")
        code = "expect(getValue()).toBe(helper(1, 2));"
        codeflash_output = transformer.transform(code); result = codeflash_output # 12.9μs -> 14.8μs (12.9% slower)

    def test_expect_call_with_no_arguments(self):
        """Test expect calls with no function arguments."""
        func = create_function_to_optimize("initialize", "initialize")
        transformer = ExpectCallTransformer(func, "capture")
        code = "expect(initialize()).toBe(true);"
        codeflash_output = transformer.transform(code); result = codeflash_output # 12.2μs -> 13.3μs (8.76% slower)

    def test_expect_call_with_whitespace_before_assertion(self):
        """Test expect calls with whitespace between closing paren and assertion."""
        func = create_function_to_optimize("func", "func")
        transformer = ExpectCallTransformer(func, "capture")
        code = "expect(func(1))  .toBe(2);"
        codeflash_output = transformer.transform(code); result = codeflash_output # 11.8μs -> 13.1μs (9.94% slower)

    def test_multiline_expect_call(self):
        """Test expect calls split across multiple lines."""
        func = create_function_to_optimize("func", "func")
        transformer = ExpectCallTransformer(func, "capture")
        code = "expect(func(1))\n  .toBe(2);"
        codeflash_output = transformer.transform(code); result = codeflash_output # 11.3μs -> 12.6μs (9.72% slower)

    def test_function_name_substring_match_avoided(self):
        """Test that function names don't match substrings."""
        func = create_function_to_optimize("add", "add")
        transformer = ExpectCallTransformer(func, "capture")
        code = "expect(address(1)).toBe('Street');"
        codeflash_output = transformer.transform(code); result = codeflash_output # 3.34μs -> 6.15μs (45.7% slower)

    def test_expect_call_with_rejects_assertion(self):
        """Test transforming an expect call with .rejects chain."""
        func = create_function_to_optimize("failFunc", "failFunc")
        transformer = ExpectCallTransformer(func, "capture")
        code = "expect(failFunc()).rejects.toThrow();"
        codeflash_output = transformer.transform(code); result = codeflash_output # 13.7μs -> 14.7μs (7.02% slower)

    def test_expect_call_with_not_resolves_chain(self):
        """Test transforming an expect call with .not.resolves chain."""
        func = create_function_to_optimize("asyncFunc", "asyncFunc")
        transformer = ExpectCallTransformer(func, "capture")
        code = "expect(asyncFunc()).not.resolves.toBe('value');"
        codeflash_output = transformer.transform(code); result = codeflash_output # 15.2μs -> 18.7μs (18.9% slower)

class TestLargeScale:
    """Test with large code volumes and performance scenarios."""

    def test_many_sequential_expect_calls(self):
        """Test transforming code with many sequential expect calls."""
        func = create_function_to_optimize("func", "func")
        transformer = ExpectCallTransformer(func, "capture")
        # Generate 100 sequential expect calls
        code_lines = [f"expect(func({i})).toBe({i+1});" for i in range(100)]
        code = "\n".join(code_lines)
        codeflash_output = transformer.transform(code); result = codeflash_output # 7.50ms -> 599μs (1151% faster)

    def test_many_expect_calls_with_complex_code(self):
        """Test transforming complex code with interspersed expect calls."""
        func = create_function_to_optimize("compute", "compute")
        transformer = ExpectCallTransformer(func, "capture")
        # Create 50 expect calls mixed with other code
        code_lines = []
        for i in range(50):
            code_lines.append(f"const x{i} = someHelper({i});")
            code_lines.append(f"expect(compute({i})).toBe({i*2});")
            code_lines.append(f"const y{i} = otherHelper({i});")
        code = "\n".join(code_lines)
        codeflash_output = transformer.transform(code); result = codeflash_output # 5.95ms -> 525μs (1033% faster)

    def test_large_function_arguments(self):
        """Test expect calls with large complex arguments."""
        func = create_function_to_optimize("process", "process")
        transformer = ExpectCallTransformer(func, "capture")
        # Create large array argument
        large_arg = ", ".join(str(i) for i in range(100))
        code = f"expect(process({large_arg})).toBe(true);"
        codeflash_output = transformer.transform(code); result = codeflash_output # 49.1μs -> 69.4μs (29.3% slower)

    def test_deeply_nested_function_calls_in_args(self):
        """Test expect calls with deeply nested function calls."""
        func = create_function_to_optimize("final", "final")
        transformer = ExpectCallTransformer(func, "capture")
        # Create deeply nested calls
        code = "expect(final(a(b(c(d(e(f(g(h(1))))))))).toBe(2);"
        codeflash_output = transformer.transform(code); result = codeflash_output # 10.1μs -> 12.7μs (20.1% slower)

    def test_many_expect_calls_in_single_string(self):
        """Test transforming many expect calls in a single code string."""
        func = create_function_to_optimize("func", "func")
        transformer = ExpectCallTransformer(func, "capture")
        # All calls in one string
        code = " ".join([f"expect(func({i})).toBe({i+1});" for i in range(200)])
        codeflash_output = transformer.transform(code); result = codeflash_output # 30.1ms -> 1.22ms (2361% faster)

    def test_code_with_very_long_function_chain(self):
        """Test expect calls with very long assertion chains."""
        func = create_function_to_optimize("async", "async")
        transformer = ExpectCallTransformer(func, "capture")
        code = "expect(async()).not.resolves.toBe(value);"
        codeflash_output = transformer.transform(code); result = codeflash_output # 15.7μs -> 16.4μs (4.39% slower)

    def test_many_calls_with_whitespace_variations(self):
        """Test many calls with varying whitespace patterns."""
        func = create_function_to_optimize("calc", "calc")
        transformer = ExpectCallTransformer(func, "capture")
        code_lines = []
        for i in range(50):
            # Vary whitespace patterns
            if i % 3 == 0:
                code_lines.append(f"expect(calc({i})).toBe({i});")
            elif i % 3 == 1:
                code_lines.append(f"  expect(calc({i}))  .toBe({i});")
            else:
                code_lines.append(f"expect(calc({i}))\n    .toBe({i});")
        code = "\n".join(code_lines)
        codeflash_output = transformer.transform(code); result = codeflash_output # 2.13ms -> 320μs (565% faster)

    def test_large_code_with_many_string_literals(self):
        """Test that string literals are properly skipped in large code."""
        func = create_function_to_optimize("func", "func")
        transformer = ExpectCallTransformer(func, "capture")
        code_lines = []
        for i in range(100):
            # Mix of real expect calls and string literals containing them
            if i % 2 == 0:
                code_lines.append(f"expect(func({i})).toBe({i});")
            else:
                code_lines.append(f"const msg = 'expect(func({i})).toBe({i});';")
        code = "\n".join(code_lines)
        codeflash_output = transformer.transform(code); result = codeflash_output # 9.88ms -> 510μs (1836% faster)

    def test_performance_with_long_code_without_matches(self):
        """Test performance when scanning long code with no expect calls."""
        func = create_function_to_optimize("func", "func")
        transformer = ExpectCallTransformer(func, "capture")
        # Create long code with no expect calls
        code_lines = [f"const x{i} = {i};" for i in range(1000)]
        code = "\n".join(code_lines)
        codeflash_output = transformer.transform(code); result = codeflash_output # 411μs -> 1.32ms (68.8% slower)

    def test_qualified_method_with_many_calls(self):
        """Test transforming many qualified method calls."""
        func = create_function_to_optimize("add", "calc.add")
        transformer = ExpectCallTransformer(func, "capture")
        # Create many method calls
        code_lines = [f"expect(calc.add({i})).toBe({i*2});" for i in range(100)]
        code = "\n".join(code_lines)
        codeflash_output = transformer.transform(code); result = codeflash_output # 8.31ms -> 289μs (2771% faster)
# codeflash_output is used to check that the output of the original code is the same as that of the optimized code.

To edit these changes git checkout codeflash/optimize-pr1318-2026-02-12T16.50.31 and push.

Codeflash Static Badge

The optimized code achieves a **6,675% speedup** (from 789ms to 11.6ms) by replacing an expensive O(n²) pattern with an O(n log n) approach for detecting whether regex matches occur inside string literals.

**Original Performance Bottleneck:**
The original code called `is_inside_string(code, pos)` for every regex match found. This function linearly scanned from position 0 to `pos` each time, checking character-by-character whether the position was inside a string literal. Line profiler shows `is_inside_string` consumed **~10 seconds** (99.6% of total time in `transform`), with ~14.6 million character checks across all calls.

**Key Optimization:**
The optimized version introduces two new methods:
1. **`_compute_string_spans(code)`** - Performs a single O(n) pass over the entire source code at the start of `transform()`, identifying all string literal regions and storing them as sorted `(start, end)` span lists
2. **`_pos_inside_spans(pos, starts, ends)`** - Uses `bisect.bisect_right()` to perform O(log n) binary search lookups against the precomputed spans

**Why This Works:**
- **Eliminates redundant scanning**: Instead of scanning the code prefix 1,641 times (once per match), we scan once and cache the results
- **Logarithmic lookups**: Each string-check becomes O(log n) binary search instead of O(n) linear scan
- **Optimal for multiple matches**: The more regex matches in the code, the greater the benefit. Test cases with 100-1000 expect calls show 1,000%+ speedups

**Test Results Show Clear Pattern:**
- Small files (1-3 matches): 7-45% slower due to upfront span computation overhead
- Medium files (50-100 matches): 565-1,151% faster
- Large files (200-1000 matches): 1,836-11,489% faster

The optimization is particularly effective for the tool's primary use case: instrumenting JavaScript test files with many `expect()` calls, where the original quadratic behavior became prohibitively expensive.
@codeflash-ai codeflash-ai bot added ⚡️ codeflash Optimization PR opened by Codeflash AI 🎯 Quality: High Optimization Quality according to Codeflash labels Feb 12, 2026
@claude
Copy link
Contributor

claude bot commented Feb 12, 2026

PR Review Summary

Prek Checks

Passed (after fix)

  • Fixed D301 ruff check error: added r prefix to docstring containing backslashes in _compute_string_spans method
  • Committed and pushed fix in 6b27ccf9
  • Verified prek passes cleanly after fix

Mypy

⚠️ 5 pre-existing errors (not introduced by this PR)

  • 4x Missing type parameters for generic type "Match" on re.Match usages (lines 192, 276, 359, 526)
  • 1x import-untyped for codeflash.code_utils.code_position
  • These exist on the base branch and are unrelated to the optimization changes

Code Review

No critical issues found

The optimization replaces an O(n²) pattern with O(n log n) by:

  1. Precomputing string literal spans once via _compute_string_spans() (single O(n) pass)
  2. Using bisect.bisect_right() for O(log n) lookups via _pos_inside_spans()

Minor observation (non-blocking): _compute_string_spans (line 761) checks for escaped quotes when entering a string (if i > 0 and code[i - 1] == "\\") while the original is_inside_string function does not check for escapes at string-open positions. This is a behavioral difference for the edge case of \" appearing outside any string context, but this is arguably a bug fix and will not affect valid JavaScript code.

All 44 existing instrumentation tests pass.

Test Coverage

File Stmts Miss Coverage
codeflash/languages/javascript/instrument.py 677 214 68%

Coverage details for optimization changes:

  • _compute_string_spans (lines 737-783): Mostly covered. Lines 762-763 (escaped quote outside string) and 772-773 (backslash escape inside string) uncovered
  • _pos_inside_spans (lines 786-797): Mostly covered. Line 795 (empty spans early return branch) uncovered
  • transform method precomputation (lines 491-505): Covered via existing tests

The uncovered lines are edge cases in string escape handling. The core optimization logic is well-tested through the 44 existing instrumentation tests which all pass.


Last updated: 2026-02-12T17:00:00Z

Comment on lines +767 to +778
i += 1
# Scan until closing quote or end
while i < n:
c = code[i]
if c == "\\" and i + 1 < n:
i += 2
continue
if c == quote_char:
i += 1
break
i += 1
end = i # position after closing quote or n if not closed
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚡️Codeflash found 10% (0.10x) speedup for ExpectCallTransformer._compute_string_spans in codeflash/languages/javascript/instrument.py

⏱️ Runtime : 725 microseconds 656 microseconds (best of 208 runs)

📝 Explanation and details

The optimized code achieves a 10% runtime improvement by replacing the character-by-character scanning for closing quotes with Python's built-in str.find() method, which is implemented in C and significantly faster for string searches.

Key optimization:
Instead of manually iterating through each character to find the closing quote:

# Original: Manual character iteration
while i < n:
    c = code[i]
    if c == "\\" and i + 1 < n:
        i += 2
        continue
    if c == quote_char:
        i += 1
        break
    i += 1

The optimized version uses str.find() to jump directly to candidate closing quotes, then counts preceding backslashes to determine if the quote is escaped:

# Optimized: Use find() + backslash counting
while True:
    q = code.find(quote_char, search_pos)
    if q == -1:
        end = n
        i = n
        break
    
    # Count backslashes before the quote
    p = q - 1
    backslash_count = 0
    while p >= start and code[p] == "\\":
        backslash_count += 1
        p -= 1
    
    # Odd number of backslashes means quote is escaped
    if backslash_count % 2 == 1:
        search_pos = q + 1
        continue
    
    end = q + 1
    i = end
    break

Why this is faster:

  1. Leverages C-level string operations: str.find() is implemented in C and optimized for substring searching, eliminating Python bytecode overhead for each character check
  2. Reduces iteration count: Instead of checking every character between quotes, we jump directly to quote candidates and only examine backslashes immediately before them
  3. Most effective for longer strings: The line profiler shows the inner while loop consumed ~40% of runtime in the original (lines with 13.7%, 13.1%, 15.2% time). The optimization dramatically reduces this overhead.

Test results confirm the optimization pattern:

  • Short strings (≤20 chars): Slight regression (0-38% slower) due to additional overhead of find() setup
  • Medium strings (100-1000 chars): Moderate gains (32-188% faster)
  • Long strings (1000+ chars): Massive gains (2519-2843% faster) as the benefit of skipping character-by-character iteration compounds

This optimization is particularly valuable for JavaScript test instrumentation where string literals can be lengthy (code snippets, error messages, template literals), making the overall 10% runtime improvement primarily driven by these longer string cases.

Correctness verification report:

Test Status
⚙️ Existing Unit Tests 🔘 None Found
🌀 Generated Regression Tests 84 Passed
⏪ Replay Tests 🔘 None Found
🔎 Concolic Coverage Tests 🔘 None Found
📊 Tests Coverage 92.6%
🌀 Click to see Generated Regression Tests
import pytest
from codeflash.discovery.functions_to_optimize import FunctionToOptimize
from codeflash.languages.javascript.instrument import ExpectCallTransformer

# Helper function to create a FunctionToOptimize instance for testing
def create_function_to_optimize(func_name: str = "testFunc") -> FunctionToOptimize:
    """Create a minimal FunctionToOptimize instance for testing."""
    return FunctionToOptimize(
        function_name=func_name,
        qualified_name=f"module.{func_name}",
        file_path="test.js",
        start_line=1,
        end_line=10
    )

def test_empty_code_string():
    """Test with empty code string - should return empty lists."""
    func_opt = create_function_to_optimize()
    transformer = ExpectCallTransformer(func_opt, "capture")
    starts, ends = transformer._compute_string_spans("") # 631ns -> 641ns (1.56% slower)

def test_code_without_strings():
    """Test with code containing no string literals - should return empty lists."""
    func_opt = create_function_to_optimize()
    transformer = ExpectCallTransformer(func_opt, "capture")
    code = "const x = 42; let y = x + 1; return y;"
    starts, ends = transformer._compute_string_spans(code) # 3.11μs -> 3.14μs (0.957% slower)

def test_single_double_quoted_string():
    """Test with a single double-quoted string."""
    func_opt = create_function_to_optimize()
    transformer = ExpectCallTransformer(func_opt, "capture")
    code = '"hello"'
    starts, ends = transformer._compute_string_spans(code) # 1.79μs -> 1.99μs (10.0% slower)

def test_single_single_quoted_string():
    """Test with a single single-quoted string."""
    func_opt = create_function_to_optimize()
    transformer = ExpectCallTransformer(func_opt, "capture")
    code = "'world'"
    starts, ends = transformer._compute_string_spans(code) # 1.68μs -> 1.95μs (13.8% slower)

def test_single_backtick_string():
    """Test with a single backtick-quoted string (template literal)."""
    func_opt = create_function_to_optimize()
    transformer = ExpectCallTransformer(func_opt, "capture")
    code = "`template`"
    starts, ends = transformer._compute_string_spans(code) # 1.85μs -> 1.98μs (6.60% slower)

def test_multiple_strings():
    """Test with multiple strings in code."""
    func_opt = create_function_to_optimize()
    transformer = ExpectCallTransformer(func_opt, "capture")
    code = '"first" + \'second\' + `third`'
    starts, ends = transformer._compute_string_spans(code) # 3.69μs -> 3.85μs (4.16% slower)

def test_empty_string():
    """Test with an empty string literal."""
    func_opt = create_function_to_optimize()
    transformer = ExpectCallTransformer(func_opt, "capture")
    code = '""'
    starts, ends = transformer._compute_string_spans(code) # 1.26μs -> 1.81μs (30.3% slower)

def test_string_with_escaped_quote_inside():
    """Test string containing escaped quotes of the same type."""
    func_opt = create_function_to_optimize()
    transformer = ExpectCallTransformer(func_opt, "capture")
    code = r'"hello\"world"'
    starts, ends = transformer._compute_string_spans(code) # 2.10μs -> 2.56μs (17.9% slower)

def test_string_with_different_escaped_quote_inside():
    """Test string with escaped quote of different type inside."""
    func_opt = create_function_to_optimize()
    transformer = ExpectCallTransformer(func_opt, "capture")
    code = r'"hello\'world"'
    starts, ends = transformer._compute_string_spans(code) # 2.17μs -> 1.96μs (10.7% faster)

def test_string_starting_with_escaped_backslash():
    """Test string that contains escaped backslash."""
    func_opt = create_function_to_optimize()
    transformer = ExpectCallTransformer(func_opt, "capture")
    code = r'"hello\\world"'
    starts, ends = transformer._compute_string_spans(code) # 2.08μs -> 1.92μs (8.32% faster)

def test_string_with_newline_escape():
    """Test string with escaped newline (\\n)."""
    func_opt = create_function_to_optimize()
    transformer = ExpectCallTransformer(func_opt, "capture")
    code = r'"line1\nline2"'
    starts, ends = transformer._compute_string_spans(code) # 2.23μs -> 1.90μs (17.3% faster)

def test_mixed_quote_types():
    """Test that different quote types don't interfere."""
    func_opt = create_function_to_optimize()
    transformer = ExpectCallTransformer(func_opt, "capture")
    code = """"inside single 'quotes' here"""
    starts, ends = transformer._compute_string_spans(code) # 2.85μs -> 1.61μs (77.1% faster)

def test_unclosed_string_at_end():
    """Test with unclosed string at end of code."""
    func_opt = create_function_to_optimize()
    transformer = ExpectCallTransformer(func_opt, "capture")
    code = 'const x = "unclosed'
    starts, ends = transformer._compute_string_spans(code) # 2.56μs -> 2.48μs (2.86% faster)

def test_escaped_quote_at_start_of_string():
    """Test with escaped quote at the very start of string content."""
    func_opt = create_function_to_optimize()
    transformer = ExpectCallTransformer(func_opt, "capture")
    code = r'"\"hello"'
    starts, ends = transformer._compute_string_spans(code) # 1.78μs -> 2.56μs (30.2% slower)

def test_escaped_quote_at_end_of_string():
    """Test with escaped quote at the very end of string content."""
    func_opt = create_function_to_optimize()
    transformer = ExpectCallTransformer(func_opt, "capture")
    code = r'"hello\""'
    starts, ends = transformer._compute_string_spans(code) # 1.80μs -> 2.56μs (29.4% slower)

def test_adjacent_strings():
    """Test with adjacent strings (no spaces between them)."""
    func_opt = create_function_to_optimize()
    transformer = ExpectCallTransformer(func_opt, "capture")
    code = '"first""second"'
    starts, ends = transformer._compute_string_spans(code) # 2.60μs -> 2.69μs (3.02% slower)

def test_backslash_at_end_of_code():
    """Test with backslash at the very end of code (no character after)."""
    func_opt = create_function_to_optimize()
    transformer = ExpectCallTransformer(func_opt, "capture")
    code = '"text\\'
    starts, ends = transformer._compute_string_spans(code) # 1.69μs -> 1.55μs (8.94% faster)

def test_single_backslash_outside_string():
    """Test backslash outside of string context."""
    func_opt = create_function_to_optimize()
    transformer = ExpectCallTransformer(func_opt, "capture")
    code = 'x = \\n "hello"'
    starts, ends = transformer._compute_string_spans(code) # 2.29μs -> 2.62μs (13.0% slower)

def test_escaped_backslash_before_quote():
    """Test escaped backslash immediately before a quote character."""
    func_opt = create_function_to_optimize()
    transformer = ExpectCallTransformer(func_opt, "capture")
    code = r'x = "\\"'
    starts, ends = transformer._compute_string_spans(code) # 1.90μs -> 2.67μs (28.8% slower)

def test_multiple_consecutive_escapes():
    """Test multiple consecutive escape sequences."""
    func_opt = create_function_to_optimize()
    transformer = ExpectCallTransformer(func_opt, "capture")
    code = r'"\\\\\\n"'
    starts, ends = transformer._compute_string_spans(code) # 1.76μs -> 1.99μs (11.5% slower)

def test_quote_escaped_by_preceding_backslash():
    """Test that a quote preceded by single backslash is not treated as string terminator."""
    func_opt = create_function_to_optimize()
    transformer = ExpectCallTransformer(func_opt, "capture")
    code = r'"text\" more"'
    starts, ends = transformer._compute_string_spans(code) # 2.11μs -> 2.54μs (16.9% slower)

def test_three_consecutive_backslashes():
    """Test three consecutive backslashes (second and third form escape pair)."""
    func_opt = create_function_to_optimize()
    transformer = ExpectCallTransformer(func_opt, "capture")
    code = r'"text\\\""'
    starts, ends = transformer._compute_string_spans(code) # 1.86μs -> 2.69μs (30.8% slower)

def test_all_three_quote_types_in_sequence():
    """Test code containing all three quote types in sequence."""
    func_opt = create_function_to_optimize()
    transformer = ExpectCallTransformer(func_opt, "capture")
    code = '"a" + \'b\' + `c`'
    starts, ends = transformer._compute_string_spans(code) # 2.88μs -> 3.79μs (24.1% slower)

def test_string_with_only_escaped_backslash():
    """Test string containing only escaped backslash."""
    func_opt = create_function_to_optimize()
    transformer = ExpectCallTransformer(func_opt, "capture")
    code = r'"\\"'
    starts, ends = transformer._compute_string_spans(code) # 1.34μs -> 2.16μs (37.9% slower)

def test_code_with_string_at_various_positions():
    """Test strings at beginning, middle, and end of code."""
    func_opt = create_function_to_optimize()
    transformer = ExpectCallTransformer(func_opt, "capture")
    code = '"start" + middle + "end"'
    starts, ends = transformer._compute_string_spans(code) # 3.10μs -> 3.59μs (13.7% slower)

def test_quote_preceded_by_non_backslash():
    """Test that quotes preceded by non-backslash characters are not treated as escaped."""
    func_opt = create_function_to_optimize()
    transformer = ExpectCallTransformer(func_opt, "capture")
    code = 'a"hello"b'
    starts, ends = transformer._compute_string_spans(code) # 2.01μs -> 2.30μs (12.6% slower)

def test_very_long_string():
    """Test with a very long string to ensure proper span calculation."""
    func_opt = create_function_to_optimize()
    transformer = ExpectCallTransformer(func_opt, "capture")
    long_content = "x" * 1000
    code = f'"{long_content}"'
    starts, ends = transformer._compute_string_spans(code) # 59.3μs -> 2.26μs (2519% faster)

def test_string_with_regex_like_pattern():
    """Test string containing regex-like content (should be treated as plain string)."""
    func_opt = create_function_to_optimize()
    transformer = ExpectCallTransformer(func_opt, "capture")
    code = r'"[a-z]+\d{2}"'
    starts, ends = transformer._compute_string_spans(code) # 2.22μs -> 1.93μs (15.0% faster)

def test_nested_quote_pairs():
    """Test that quotes of different types can appear inside strings."""
    func_opt = create_function_to_optimize()
    transformer = ExpectCallTransformer(func_opt, "capture")
    code = '''"`'nested'`"'''
    starts, ends = transformer._compute_string_spans(code) # 1.88μs -> 1.98μs (4.99% slower)

def test_many_short_strings():
    """Test with 100 short strings to verify scalability."""
    func_opt = create_function_to_optimize()
    transformer = ExpectCallTransformer(func_opt, "capture")
    # Build code with many short strings: "a" + "b" + "c" + ...
    parts = [f'"{chr(97 + i % 26)}"' for i in range(100)]
    code = " + ".join(parts)
    starts, ends = transformer._compute_string_spans(code) # 46.0μs -> 53.3μs (13.8% slower)
    # All strings should have same length (3 chars each, content span 1-3)
    for i, (start, end) in enumerate(zip(starts, ends)):
        pass

def test_many_long_strings():
    """Test with 50 longer strings."""
    func_opt = create_function_to_optimize()
    transformer = ExpectCallTransformer(func_opt, "capture")
    # Create 50 strings of 20 characters each
    parts = [f'"{chr(97 + i % 26) * 20}"' for i in range(50)]
    code = " + ".join(parts)
    starts, ends = transformer._compute_string_spans(code) # 80.3μs -> 27.9μs (188% faster)
    # Each string has 20 characters of content
    for start, end in zip(starts, ends):
        pass

def test_alternating_quote_types_large():
    """Test with 100 strings alternating between three quote types."""
    func_opt = create_function_to_optimize()
    transformer = ExpectCallTransformer(func_opt, "capture")
    quotes = ['"', "'", "`"]
    parts = []
    for i in range(100):
        quote = quotes[i % 3]
        content = f"str{i}"
        parts.append(f"{quote}{content}{quote}")
    code = " + ".join(parts)
    starts, ends = transformer._compute_string_spans(code) # 73.0μs -> 54.6μs (33.8% faster)

def test_dense_escaped_sequences():
    """Test string with many consecutive escaped characters."""
    func_opt = create_function_to_optimize()
    transformer = ExpectCallTransformer(func_opt, "capture")
    # Create a string with many escaped characters: "\\" repeated
    code = '"' + (r'\\' * 100) + '"'
    starts, ends = transformer._compute_string_spans(code) # 7.42μs -> 13.2μs (43.8% slower)

def test_alternating_escaped_and_unescaped():
    """Test string alternating between escaped and unescaped patterns."""
    func_opt = create_function_to_optimize()
    transformer = ExpectCallTransformer(func_opt, "capture")
    # Pattern: \\ + regular char + \\ + regular char ... (1000 iterations)
    pattern = (r'\\' + 'x') * 500
    code = f'"{pattern}"'
    starts, ends = transformer._compute_string_spans(code) # 65.2μs -> 2.21μs (2843% faster)

def test_many_strings_with_mixed_escapes():
    """Test 100 strings each containing various escape sequences."""
    func_opt = create_function_to_optimize()
    transformer = ExpectCallTransformer(func_opt, "capture")
    parts = []
    for i in range(100):
        # Each string: "before\nafter"
        escaped_content = f"line{i}" + r"\n" + f"line{i+1}"
        parts.append(f'"{escaped_content}"')
    code = " + ".join(parts)
    starts, ends = transformer._compute_string_spans(code) # 122μs -> 53.9μs (127% faster)

def test_deeply_nested_quote_patterns():
    """Test code with multiple levels of string nesting patterns."""
    func_opt = create_function_to_optimize()
    transformer = ExpectCallTransformer(func_opt, "capture")
    # Create a pattern like: "a'b`c'd"e'f"
    # Each quote type is used inside other quote types
    code = '''"a'b`c'd"e'f"g`h'i"j'''
    starts, ends = transformer._compute_string_spans(code) # 3.17μs -> 3.49μs (9.21% slower)

def test_1000_character_code_with_strings():
    """Test with ~1000 character code containing multiple strings."""
    func_opt = create_function_to_optimize()
    transformer = ExpectCallTransformer(func_opt, "capture")
    # Build code: const str1 = "content1"; const str2 = "content2"; ... (repeated)
    code_parts = []
    for i in range(50):
        code_parts.append(f'const str{i} = "content{i}";')
    code = " ".join(code_parts)
    starts, ends = transformer._compute_string_spans(code) # 85.8μs -> 65.0μs (32.0% faster)

def test_string_positions_validity():
    """Test that all computed string positions are valid indices."""
    func_opt = create_function_to_optimize()
    transformer = ExpectCallTransformer(func_opt, "capture")
    code = 'const a = "first"; let b = \'second\'; var c = `third`;'
    starts, ends = transformer._compute_string_spans(code) # 5.06μs -> 5.30μs (4.53% slower)
    # Verify all positions are valid
    for start, end in zip(starts, ends):
        pass

def test_string_contents_extraction():
    """Test that computed spans correctly identify string contents."""
    func_opt = create_function_to_optimize()
    transformer = ExpectCallTransformer(func_opt, "capture")
    code = 'var x = "hello world";'
    starts, ends = transformer._compute_string_spans(code) # 2.75μs -> 2.71μs (1.10% faster)
    # Extract using computed spans and verify
    for start, end in zip(starts, ends):
        content = code[start:end]

def test_performance_with_many_escaped_quotes():
    """Test performance with code containing many escaped quotes."""
    func_opt = create_function_to_optimize()
    transformer = ExpectCallTransformer(func_opt, "capture")
    # Create 100 strings, each with 10 escaped quotes inside
    parts = []
    for i in range(100):
        content = r'\"' * 10
        parts.append(f'"{content}"')
    code = " + ".join(parts)
    # Should complete quickly even with complex escape handling
    starts, ends = transformer._compute_string_spans(code) # 111μs -> 301μs (63.2% slower)

def test_quote_at_position_zero():
    """Test string that starts at position 0."""
    func_opt = create_function_to_optimize()
    transformer = ExpectCallTransformer(func_opt, "capture")
    code = '"start of code'
    starts, ends = transformer._compute_string_spans(code) # 2.11μs -> 1.63μs (29.5% faster)

def test_consecutive_escape_pairs_at_boundary():
    """Test escape sequences right at string boundaries."""
    func_opt = create_function_to_optimize()
    transformer = ExpectCallTransformer(func_opt, "capture")
    code = r'"\\\\\\\\\\\\\\\\\\\\\"'  # Many escaped backslashes before closing quote
    starts, ends = transformer._compute_string_spans(code) # 1.99μs -> 3.58μs (44.3% slower)
# codeflash_output is used to check that the output of the original code is the same as that of the optimized code.

To test or edit this optimization locally git merge codeflash/optimize-pr1469-2026-02-12T17.18.54

Click to see suggested changes
Suggested change
i += 1
# Scan until closing quote or end
while i < n:
c = code[i]
if c == "\\" and i + 1 < n:
i += 2
continue
if c == quote_char:
i += 1
break
i += 1
end = i # position after closing quote or n if not closed
search_pos = start
while True:
q = code.find(quote_char, search_pos)
if q == -1:
end = n
i = n
break
p = q - 1
backslash_count = 0
while p >= start and code[p] == "\\":
backslash_count += 1
p -= 1
if backslash_count % 2 == 1:
search_pos = q + 1
continue
end = q + 1
i = end
break

Static Badge

@codeflash-ai codeflash-ai bot closed this Feb 13, 2026
@codeflash-ai
Copy link
Contributor Author

codeflash-ai bot commented Feb 13, 2026

This PR has been automatically closed because the original PR #1318 by mohammedahmed18 was closed.

Base automatically changed from fix/js-jest30-loop-runner to main February 13, 2026 11:46
@codeflash-ai codeflash-ai bot deleted the codeflash/optimize-pr1318-2026-02-12T16.50.31 branch February 13, 2026 11:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

⚡️ codeflash Optimization PR opened by Codeflash AI 🎯 Quality: High Optimization Quality according to Codeflash

Projects

None yet

Development

Successfully merging this pull request may close these issues.

0 participants