1616
1717import logging
1818import re
19+ from functools import lru_cache
1920from pathlib import Path
2021from 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