Commit f9c59b6
authored
Optimize _add_behavior_instrumentation
This optimization achieves a **22% runtime improvement** (4.44ms → 3.63ms) by addressing three key performance bottlenecks:
## Primary Optimization: Cached Regex Compilation (29.7% of optimized runtime)
The original code compiled the same regex pattern 202 times inside a loop (consuming 17.8% of runtime). The optimized version introduces:
```python
@lru_cache(maxsize=128)
def _get_method_call_pattern(func_name: str):
return re.compile(...)
```
This caches compiled patterns, eliminating redundant compilation. While the first call appears slower in the line profiler (9.3ms vs 8.3ms total), this is because it includes cache initialization overhead. Subsequent calls benefit from instant retrieval, making this optimization particularly valuable when:
- Instrumenting multiple test methods in sequence
- Processing classes with many `@Test` methods (e.g., the 50-method test shows 14.8% speedup)
## Secondary Optimization: Efficient Brace Counting
The original code iterated character-by-character through method bodies (23.4% of runtime):
```python
for ch in body_line:
if ch == "{": brace_depth += 1
elif ch == "}": brace_depth -= 1
```
The optimized version uses Python's built-in string methods:
```python
open_count = body_line.count('{')
close_count = body_line.count('}')
brace_depth += open_count - close_count
```
This change shows dramatic improvements in tests with deeply nested structures:
- 10-level nested braces: 66.4% faster
- Large method bodies (100+ lines): 44.0% faster
- Methods with many variables (500+): 88.9% faster
## Performance Characteristics
The optimization excels in scenarios common to Java test instrumentation:
- **Multiple test methods**: 11-15% speedup for classes with 30-100 test methods
- **Complex method bodies**: 29-44% speedup for methods with many nested structures or statements
- **Sequential processing**: Benefits accumulate when instrumenting multiple files due to regex caching
The minor slowdowns (3-9%) in trivial cases (empty methods, minimal source) are negligible compared to the substantial gains in realistic workloads, where Java test classes typically contain multiple complex test methods.1 parent c587c47 commit f9c59b6
1 file changed
Lines changed: 29 additions & 16 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
16 | 16 | | |
17 | 17 | | |
18 | 18 | | |
| 19 | + | |
19 | 20 | | |
20 | 21 | | |
21 | 22 | | |
| |||
257 | 258 | | |
258 | 259 | | |
259 | 260 | | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
260 | 265 | | |
261 | 266 | | |
262 | 267 | | |
| |||
299 | 304 | | |
300 | 305 | | |
301 | 306 | | |
302 | | - | |
303 | | - | |
304 | | - | |
305 | | - | |
306 | | - | |
| 307 | + | |
| 308 | + | |
| 309 | + | |
| 310 | + | |
| 311 | + | |
307 | 312 | | |
308 | 313 | | |
309 | 314 | | |
| |||
318 | 323 | | |
319 | 324 | | |
320 | 325 | | |
321 | | - | |
322 | | - | |
323 | | - | |
324 | | - | |
325 | | - | |
326 | | - | |
327 | | - | |
328 | | - | |
329 | | - | |
330 | | - | |
331 | | - | |
332 | 326 | | |
333 | 327 | | |
334 | 328 | | |
| |||
726 | 720 | | |
727 | 721 | | |
728 | 722 | | |
| 723 | + | |
| 724 | + | |
| 725 | + | |
| 726 | + | |
| 727 | + | |
| 728 | + | |
| 729 | + | |
| 730 | + | |
| 731 | + | |
| 732 | + | |
| 733 | + | |
| 734 | + | |
| 735 | + | |
| 736 | + | |
| 737 | + | |
| 738 | + | |
| 739 | + | |
| 740 | + | |
| 741 | + | |
0 commit comments