Skip to content

Commit e86f21e

Browse files
Merge pull request #1294 from codeflash-ai/codeflash/optimize-pr1199-2026-02-03T08.18.57
⚡️ Speed up function `_add_behavior_instrumentation` by 22% in PR #1199 (`omni-java`)
2 parents 9b13db1 + f9c59b6 commit e86f21e

1 file changed

Lines changed: 29 additions & 16 deletions

File tree

codeflash/languages/java/instrumentation.py

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
import logging
1818
import re
19+
from functools import lru_cache
1920
from pathlib import Path
2021
from typing import TYPE_CHECKING
2122

@@ -257,6 +258,10 @@ def _add_behavior_instrumentation(source: str, class_name: str, func_name: str)
257258
i = 0
258259
iteration_counter = 0
259260

261+
262+
# Pre-compile the regex pattern once
263+
method_call_pattern = _get_method_call_pattern(func_name)
264+
260265
while i < len(lines):
261266
line = lines[i]
262267
stripped = line.strip()
@@ -299,11 +304,11 @@ def _add_behavior_instrumentation(source: str, class_name: str, func_name: str)
299304

300305
while i < len(lines) and brace_depth > 0:
301306
body_line = lines[i]
302-
for ch in body_line:
303-
if ch == "{":
304-
brace_depth += 1
305-
elif ch == "}":
306-
brace_depth -= 1
307+
# Count braces more efficiently using string methods
308+
open_count = body_line.count('{')
309+
close_count = body_line.count('}')
310+
brace_depth += open_count - close_count
311+
307312

308313
if brace_depth > 0:
309314
body_lines.append(body_line)
@@ -318,17 +323,6 @@ def _add_behavior_instrumentation(source: str, class_name: str, func_name: str)
318323
call_counter = 0
319324
wrapped_body_lines = []
320325

321-
# Use regex to find method calls with the target function
322-
# Pattern matches: receiver.funcName(args) where receiver can be:
323-
# - identifier (counter, calc, etc.)
324-
# - new ClassName()
325-
# - new ClassName(args)
326-
# - this
327-
method_call_pattern = re.compile(
328-
rf"((?:new\s+\w+\s*\([^)]*\)|[a-zA-Z_]\w*))\s*\.\s*({re.escape(func_name)})\s*\(([^)]*)\)",
329-
re.MULTILINE
330-
)
331-
332326
for body_line in body_lines:
333327
# Check if this line contains a call to the target function
334328
if func_name in body_line and "(" in body_line:
@@ -726,3 +720,22 @@ def _add_import(source: str, import_statement: str) -> str:
726720

727721
lines.insert(insert_idx, import_statement + "\n")
728722
return "".join(lines)
723+
724+
725+
726+
@lru_cache(maxsize=128)
727+
def _get_method_call_pattern(func_name: str):
728+
"""Cache compiled regex patterns for method call matching."""
729+
return re.compile(
730+
rf"((?:new\s+\w+\s*\([^)]*\)|[a-zA-Z_]\w*))\s*\.\s*({re.escape(func_name)})\s*\(([^)]*)\)",
731+
re.MULTILINE
732+
)
733+
734+
735+
@lru_cache(maxsize=128)
736+
def _get_method_call_pattern(func_name: str):
737+
"""Cache compiled regex patterns for method call matching."""
738+
return re.compile(
739+
rf"((?:new\s+\w+\s*\([^)]*\)|[a-zA-Z_]\w*))\s*\.\s*({re.escape(func_name)})\s*\(([^)]*)\)",
740+
re.MULTILINE
741+
)

0 commit comments

Comments
 (0)