codeflash-ai[bot]
238de2b9c0
⚡ ️ Speed up function _safe_replace_function_calls by 101% in PR #2247 (multi-language) ( #2306 )
...
## ⚡ ️ This pull request contains optimizations for PR #2247
If you approve this dependent PR, these changes will be merged into the
original PR branch `multi-language`.
>This PR will be automatically closed if the original PR is merged.
----
#### 📄 101% (1.01x) speedup for ***`_safe_replace_function_calls` in
`django/aiservice/testgen/instrumentation/javascript/instrument_javascript.py`***
⏱️ Runtime : **`4.63 milliseconds`** **→** **`2.30 milliseconds`** (best
of `5` runs)
#### 📝 Explanation and details
This optimization achieves a **101% speedup** (reducing runtime from
4.63ms to 2.30ms) through three key performance improvements:
**1. Pre-compiled regex pattern (primary speedup driver)**
The original code calls `re.match(pattern, remaining)` inside the main
loop, which recompiles the pattern on every iteration. The line profiler
shows this operation took 3.92 seconds (33.1% of total time). The
optimized version compiles the pattern once upfront (`pattern_re =
re.compile(pattern)`) and reuses it via `pattern_re.match(source, i)`,
reducing this to 1.10 seconds (10% of total time) - a **72% reduction**
in pattern matching overhead.
**2. Eliminated substring creation for pattern matching**
The original creates a new substring `remaining = source[i:]` on every
loop iteration (5.68e8 ns, 4.8% of time). The optimized version uses
`pattern_re.match(source, i)` which matches directly from position `i`
without creating intermediate strings. This saves both allocation and
copying costs, particularly impactful given the loop runs 1.3-1.8
million times per execution.
**3. Fast-forward through comments using `str.find()`**
- **Single-line comments**: Changed from character-by-character loop
(`while i < length and source[i] != "\n"`) to `source.find("\n", i)`,
eliminating repeated indexing operations
- **Multi-line comments**: Changed from character-by-character scanning
to `source.find("*/", i)`, directly jumping to comment end
**Test case analysis:**
The optimization particularly excels on:
- **Large-scale scenarios**: The 500-iteration test shows 125% speedup
(1.24ms → 549μs), demonstrating excellent scalability
- **Comment-heavy code**: Tests with comments show 17-65% speedups as
`str.find()` efficiently skips entire comment blocks
- **Method call filtering**: Tests with `.foo()` patterns show 53-75%
speedups from reduced pattern matching overhead
All 45 test cases pass with improved or comparable performance,
validating that the optimization preserves correctness while delivering
consistent runtime improvements across diverse JavaScript code patterns.
✅ **Correctness verification report:**
| Test | Status |
| --------------------------- | ----------------- |
| ⚙️ Existing Unit Tests | 🔘 **None Found** |
| 🌀 Generated Regression Tests | ✅ **59 Passed** |
| ⏪ Replay Tests | 🔘 **None Found** |
| 🔎 Concolic Coverage Tests | 🔘 **None Found** |
|📊 Tests Coverage | 100.0% |
<details>
<summary>🌀 Click to see Generated Regression Tests</summary>
```python
from __future__ import annotations
# imports
import re
from collections.abc import Callable
import pytest # used for our unit tests
from testgen.instrumentation.javascript.instrument_javascript import (
_safe_replace_function_calls,
)
# unit tests
# Helper replacement function used in many tests: wraps the matched call
def _wrap_with_capture(match: re.Match[str]) -> str:
# return a wrapper that includes the exact matched text
return f"codeflash.capture({match.group(0)})"
def test_basic_single_replacement():
# Basic scenario: a single top-level function call should be replaced.
src = "var x = foo(1);"
pattern = r"foo\([^\)]*\)"
codeflash_output = _safe_replace_function_calls(
src, "foo", _wrap_with_capture, pattern
)
res = codeflash_output # 12.5μs -> 9.73μs (28.9% faster)
def test_string_literals_not_replaced():
# Strings containing the function call should be left alone.
src = "var s = 'foo(1)'; foo(2);"
pattern = r"foo\([^\)]*\)"
codeflash_output = _safe_replace_function_calls(
src, "foo", _wrap_with_capture, pattern
)
res = codeflash_output # 14.5μs -> 10.5μs (38.5% faster)
def test_double_quoted_and_escaped_strings():
# Double-quoted strings with escaped quotes should be preserved.
src = 'var s = "he said \\"foo(3)\\""; foo(4);'
pattern = r"foo\([^\)]*\)"
codeflash_output = _safe_replace_function_calls(
src, "foo", _wrap_with_capture, pattern
)
res = codeflash_output # 15.7μs -> 11.5μs (36.0% faster)
def test_template_literals_with_expressions_skipped():
# Template literals should preserve content and skip replacement inside ${...}.
src = "`start ${ { nested: foo(5) } } end` foo(6);"
pattern = r"foo\([^\)]*\)"
codeflash_output = _safe_replace_function_calls(
src, "foo", _wrap_with_capture, pattern
)
res = codeflash_output # 12.8μs -> 12.0μs (6.23% faster)
def test_single_line_comment_skipped():
# Calls inside single-line comments should not be replaced.
src = " // foo(7)\nfoo(8);"
pattern = r"foo\([^\)]*\)"
codeflash_output = _safe_replace_function_calls(
src, "foo", _wrap_with_capture, pattern
)
res = codeflash_output # 9.40μs -> 8.02μs (17.1% faster)
def test_multi_line_comment_skipped_and_unclosed_comment_handled():
# Multi-line comments containing calls should be preserved.
src = "/* comment foo(9) */ foo(10);"
pattern = r"foo\([^\)]*\)"
codeflash_output = _safe_replace_function_calls(
src, "foo", _wrap_with_capture, pattern
)
res = codeflash_output # 9.87μs -> 8.17μs (20.9% faster)
# Unclosed multi-line comment at EOF should not hang or replace inside it
src_unclosed = "/* unclosed foo(11)"
codeflash_output = _safe_replace_function_calls(
src_unclosed, "foo", _wrap_with_capture, pattern
)
res_unclosed = codeflash_output # 3.26μs -> 1.97μs (65.2% faster)
def test_method_call_dot_not_replaced():
# Calls that are methods (preceded by a dot) should not be replaced.
src = "obj.foo(12); foo(13);"
pattern = r"foo\([^\)]*\)"
codeflash_output = _safe_replace_function_calls(
src, "foo", _wrap_with_capture, pattern
)
res = codeflash_output # 14.8μs -> 9.49μs (55.6% faster)
def test_already_wrapped_skipped():
# If 'codeflash.capture' immediately precedes the call start index, replacement should be skipped.
# This tests the specific check in the function that avoids double-wrapping.
src = "codeflash.capturefoo(14);"
pattern = r"foo\([^\)]*\)"
codeflash_output = _safe_replace_function_calls(
src, "foo", _wrap_with_capture, pattern
)
res = codeflash_output # 19.9μs -> 11.3μs (75.4% faster)
def test_template_literal_with_escaped_backticks_and_expressions():
# Template literal containing escaped backticks and ${...} should be handled correctly.
src = r"`backtick \` and ${ { foo: foo(15) } } end` foo(16);"
pattern = r"foo\([^\)]*\)"
codeflash_output = _safe_replace_function_calls(
src, "foo", _wrap_with_capture, pattern
)
res = codeflash_output # 14.2μs -> 12.8μs (11.1% faster)
def test_overlapping_names_handling():
# When function name appears adjacent to other letters like 'foofoo(17)',
# ensure inner call is still found at the correct position.
src = "foofoo(17); foo(18);"
pattern = r"foo\([^\)]*\)"
codeflash_output = _safe_replace_function_calls(
src, "foo", _wrap_with_capture, pattern
)
res = codeflash_output # 11.3μs -> 7.92μs (43.2% faster)
def test_escaped_characters_in_regular_strings_do_not_break_scanning():
# Strings that include escaped backslashes and escaped quotes should not break the scanner.
src = '"line1\\nfoo(19)\\nline2" foo(20);'
pattern = r"foo\([^\)]*\)"
codeflash_output = _safe_replace_function_calls(
src, "foo", _wrap_with_capture, pattern
)
res = codeflash_output # 10.9μs -> 9.15μs (19.1% faster)
def test_large_scale_multiple_replacements():
# Large-scale test: many occurrences (but below 1000) to assess scalability.
count = 500 # keep below 1000 as per instructions
src_parts = [f"foo({i});" for i in range(count)]
src = " ".join(src_parts)
pattern = r"foo\([^\)]*\)"
codeflash_output = _safe_replace_function_calls(
src, "foo", _wrap_with_capture, pattern
)
res = codeflash_output # 1.24ms -> 549μs (125% faster)
# Ensure that every occurrence was wrapped exactly once.
wrapped_count = res.count("codeflash.capture(")
def test_no_false_positive_when_preceded_by_other_chars():
# If the character before the function is not a dot but some other char, replacement should proceed.
src = "afoo(21); .foo(22);"
pattern = r"foo\([^\)]*\)"
codeflash_output = _safe_replace_function_calls(
src, "foo", _wrap_with_capture, pattern
)
res = codeflash_output # 13.3μs -> 8.96μs (48.5% faster)
def test_pattern_that_matches_functions_with_spaces():
# Pattern that allows spaces before the opening parenthesis.
src = "foo (23); foo(24);"
pattern = r"foo\s*\([^\)]*\)"
codeflash_output = _safe_replace_function_calls(
src, "foo", _wrap_with_capture, pattern
)
res = codeflash_output # 10.1μs -> 7.71μs (30.6% faster)
# codeflash_output is used to check that the output of the original code is the same as that of the optimized code.
```
```python
import re
from collections.abc import Callable
# imports
import pytest
from testgen.instrumentation.javascript.instrument_javascript import (
_safe_replace_function_calls,
)
class TestBasicFunctionality:
"""Test cases for basic functionality of _safe_replace_function_calls."""
def test_simple_function_call_replacement(self):
"""Test replacing a simple function call with a basic pattern."""
source = "foo()"
pattern = r"foo\(\)"
replace_func = lambda m: "bar()"
codeflash_output = _safe_replace_function_calls(
source, "foo", replace_func, pattern
)
result = codeflash_output # 5.38μs -> 5.00μs (7.58% faster)
def test_function_call_with_arguments(self):
"""Test replacing function calls that have arguments."""
source = "foo(x, y)"
pattern = r"foo\([^)]*\)"
replace_func = lambda m: "bar(x, y)"
codeflash_output = _safe_replace_function_calls(
source, "foo", replace_func, pattern
)
result = codeflash_output # 5.25μs -> 4.74μs (10.7% faster)
def test_multiple_function_calls(self):
"""Test replacing multiple occurrences of the same function."""
source = "foo(); foo(); foo();"
pattern = r"foo\(\)"
call_count = [0]
def replace_func(m):
call_count[0] += 1
return f"replaced_{call_count[0]}()"
codeflash_output = _safe_replace_function_calls(
source, "foo", replace_func, pattern
)
result = codeflash_output # 12.2μs -> 8.70μs (40.3% faster)
def test_empty_source(self):
"""Test with an empty source string."""
source = ""
pattern = r"foo\(\)"
replace_func = lambda m: "bar()"
codeflash_output = _safe_replace_function_calls(
source, "foo", replace_func, pattern
)
result = codeflash_output # 853ns -> 2.04μs (58.1% slower)
def test_no_matches(self):
"""Test when no function calls match the pattern."""
source = "let x = 5; y = 10;"
pattern = r"foo\(\)"
replace_func = lambda m: "bar()"
codeflash_output = _safe_replace_function_calls(
source, "foo", replace_func, pattern
)
result = codeflash_output # 14.3μs -> 7.73μs (84.8% faster)
def test_function_call_in_middle_of_code(self):
"""Test replacing function call in the middle of other code."""
source = "let result = foo(); console.log(result);"
pattern = r"foo\(\)"
replace_func = lambda m: "bar()"
codeflash_output = _safe_replace_function_calls(
source, "foo", replace_func, pattern
)
result = codeflash_output # 24.9μs -> 12.9μs (92.5% faster)
def test_preserve_whitespace(self):
"""Test that whitespace around function calls is preserved."""
source = "foo( ) "
pattern = r"foo\(\s*\)"
replace_func = lambda m: "bar()"
codeflash_output = _safe_replace_function_calls(
source, "foo", replace_func, pattern
)
result = codeflash_output # 6.61μs -> 5.85μs (12.8% faster)
def test_function_call_in_single_quoted_string(self):
"""Test that function calls inside single-quoted strings are NOT replaced."""
source = "'foo()' bar()"
pattern = r"foo\(\)|bar\(\)"
replace_func = lambda m: "replaced()"
codeflash_output = _safe_replace_function_calls(
source, "foo", replace_func, pattern
)
result = codeflash_output # 7.49μs -> 7.27μs (3.07% faster)
def test_function_call_in_double_quoted_string(self):
"""Test that function calls inside double-quoted strings are NOT replaced."""
source = '"foo()" bar()'
pattern = r"foo\(\)|bar\(\)"
replace_func = lambda m: "replaced()"
codeflash_output = _safe_replace_function_calls(
source, "foo", replace_func, pattern
)
result = codeflash_output # 7.22μs -> 6.47μs (11.5% faster)
def test_function_call_in_single_line_comment(self):
"""Test that function calls in single-line comments are NOT replaced."""
source = "// foo() should not be replaced\nbar()"
pattern = r"foo\(\)|bar\(\)"
replace_func = lambda m: "replaced()"
codeflash_output = _safe_replace_function_calls(
source, "foo", replace_func, pattern
)
result = codeflash_output # 9.27μs -> 6.86μs (35.0% faster)
def test_function_call_in_multiline_comment(self):
"""Test that function calls in multi-line comments are NOT replaced."""
source = "/* foo() should not be replaced */ bar()"
pattern = r"foo\(\)|bar\(\)"
replace_func = lambda m: "replaced()"
codeflash_output = _safe_replace_function_calls(
source, "foo", replace_func, pattern
)
result = codeflash_output # 10.00μs -> 7.47μs (33.7% faster)
class TestEdgeCases:
"""Test cases for edge cases and unusual conditions."""
def test_escaped_quote_in_string(self):
"""Test handling of escaped quotes inside strings."""
source = r"'foo\'s bar' foo()"
pattern = r"foo\(\)"
replace_func = lambda m: "replaced()"
codeflash_output = _safe_replace_function_calls(
source, "foo", replace_func, pattern
)
result = codeflash_output # 7.99μs -> 7.41μs (7.81% faster)
def test_escaped_backslash_in_string(self):
"""Test handling of escaped backslashes in strings."""
source = r"'path\\to\\file' foo()"
pattern = r"foo\(\)"
replace_func = lambda m: "replaced()"
codeflash_output = _safe_replace_function_calls(
source, "foo", replace_func, pattern
)
result = codeflash_output # 8.41μs -> 7.76μs (8.36% faster)
def test_method_call_not_replaced(self):
"""Test that method calls (preceded by dot) are NOT replaced."""
source = "obj.foo() + foo()"
pattern = r"foo\(\)"
replace_func = lambda m: "bar()"
codeflash_output = _safe_replace_function_calls(
source, "foo", replace_func, pattern
)
result = codeflash_output # 12.9μs -> 8.37μs (53.6% faster)
def test_template_literal_simple(self):
"""Test handling of simple template literals."""
source = "`foo()` bar()"
pattern = r"foo\(\)|bar\(\)"
replace_func = lambda m: "replaced()"
codeflash_output = _safe_replace_function_calls(
source, "foo", replace_func, pattern
)
result = codeflash_output # 7.53μs -> 6.86μs (9.73% faster)
def test_template_literal_with_expression(self):
"""Test handling of template literals with embedded expressions."""
source = "`Value: ${foo()}` bar()"
pattern = r"foo\(\)|bar\(\)"
replace_func = lambda m: "replaced()"
codeflash_output = _safe_replace_function_calls(
source, "foo", replace_func, pattern
)
result = codeflash_output # 9.50μs -> 9.01μs (5.51% faster)
def test_template_literal_with_nested_braces(self):
"""Test handling of template literals with nested braces in expressions."""
source = "`${obj.map(x => ({value: x}))}` foo()"
pattern = r"foo\(\)"
replace_func = lambda m: "replaced()"
codeflash_output = _safe_replace_function_calls(
source, "foo", replace_func, pattern
)
result = codeflash_output # 10.5μs -> 10.2μs (2.83% faster)
def test_codeflash_capture_already_wrapped(self):
"""Test that already wrapped codeflash.capture calls are not double-wrapped."""
source = "codeflash.capture(foo())"
pattern = r"foo\(\)"
replace_func = lambda m: "bar()"
codeflash_output = _safe_replace_function_calls(
source, "foo", replace_func, pattern
)
result = codeflash_output # 16.9μs -> 10.5μs (60.3% faster)
def test_nested_quotes_in_string(self):
"""Test handling of different quote types inside strings."""
source = """'string with "quotes"' foo()"""
pattern = r"foo\(\)"
replace_func = lambda m: "replaced()"
codeflash_output = _safe_replace_function_calls(
source, "foo", replace_func, pattern
)
result = codeflash_output # 8.46μs -> 8.08μs (4.66% faster)
def test_multiline_comment_not_closed(self):
"""Test handling of multi-line comment that extends to end of source."""
source = "foo() /* comment starts here"
pattern = r"foo\(\)"
replace_func = lambda m: "replaced()"
codeflash_output = _safe_replace_function_calls(
source, "foo", replace_func, pattern
)
result = codeflash_output # 9.20μs -> 6.59μs (39.5% faster)
def test_function_call_at_end_of_source(self):
"""Test function call replacement at the very end of source."""
source = "foo()"
pattern = r"foo\(\)"
replace_func = lambda m: "bar()"
codeflash_output = _safe_replace_function_calls(
source, "foo", replace_func, pattern
)
result = codeflash_output # 4.28μs -> 4.24μs (0.730% faster)
def test_adjacent_strings(self):
"""Test handling of adjacent string literals."""
source = "'foo()' \"foo()\" foo()"
pattern = r"foo\(\)"
replace_func = lambda m: "replaced()"
codeflash_output = _safe_replace_function_calls(
source, "foo", replace_func, pattern
)
result = codeflash_output # 9.13μs -> 8.05μs (13.4% faster)
def test_comment_after_function_call(self):
"""Test function call followed by a comment."""
source = "foo() // this is foo\nbar()"
pattern = r"foo\(\)|bar\(\)"
replace_func = lambda m: "replaced()"
codeflash_output = _safe_replace_function_calls(
source, "foo", replace_func, pattern
)
result = codeflash_output # 10.5μs -> 8.23μs (27.1% faster)
def test_escaped_character_in_comment(self):
"""Test comment handling with special characters."""
source = "foo() // comment with \\ backslash\nbar()"
pattern = r"foo\(\)|bar\(\)"
replace_func = lambda m: "replaced()"
codeflash_output = _safe_replace_function_calls(
source, "foo", replace_func, pattern
)
result = codeflash_output # 11.4μs -> 8.42μs (35.6% faster)
def test_regex_special_chars_in_string(self):
"""Test strings containing regex special characters."""
source = r"'[a-z].*+?' foo()"
pattern = r"foo\(\)"
replace_func = lambda m: "replaced()"
codeflash_output = _safe_replace_function_calls(
source, "foo", replace_func, pattern
)
result = codeflash_output # 7.29μs -> 6.70μs (8.80% faster)
def test_single_char_string(self):
"""Test handling of single character strings."""
source = "'' 'a' foo()"
pattern = r"foo\(\)"
replace_func = lambda m: "replaced()"
codeflash_output = _safe_replace_function_calls(
source, "foo", replace_func, pattern
)
result = codeflash_output # 7.62μs -> 6.79μs (12.2% faster)
def test_consecutive_escaped_quotes(self):
"""Test multiple consecutive escaped quotes."""
source = r"'\"\"' foo()"
pattern = r"foo\(\)"
replace_func = lambda m: "replaced()"
codeflash_output = _safe_replace_function_calls(
source, "foo", replace_func, pattern
)
result = codeflash_output # 6.92μs -> 6.09μs (13.5% faster)
def test_template_literal_escaped_backtick(self):
"""Test template literal with escaped backtick."""
source = r"`escaped \` backtick` foo()"
pattern = r"foo\(\)"
replace_func = lambda m: "replaced()"
codeflash_output = _safe_replace_function_calls(
source, "foo", replace_func, pattern
)
result = codeflash_output # 9.10μs -> 8.80μs (3.43% faster)
```
</details>
To edit these changes `git checkout
codeflash/optimize-pr2247-2026-01-25T08.38.25` and push.
[](https://codeflash.ai )

Co-authored-by: codeflash-ai[bot] <148906541+codeflash-ai[bot]@users.noreply.github.com>
Co-authored-by: Kevin Turcios <106575910+KRRT7@users.noreply.github.com>
2026-01-27 00:28:03 -05:00