Commit 7cd5cda
authored
Optimize _add_timing_instrumentation
This optimization achieves a **15% runtime improvement** (10.2ms → 8.81ms) by replacing recursive AST traversal with iterative stack-based traversal in two critical functions: `collect_test_methods` and `collect_target_calls`.
## Key Changes
**1. Iterative AST Traversal (Primary Speedup)**
- Replaced recursive tree walking with explicit stack-based iteration
- In `collect_test_methods`: Changed from recursive calls to `while stack` loop with `stack.extend(reversed(current.children))`
- In `collect_target_calls`: Similar transformation using explicit stack management
- **Impact**: Line profiler shows `collect_test_methods` dropped from 24.2% to 3.8% of total runtime (81% reduction in that function)
**2. Why This Works in Python**
- Python function calls have significant overhead (frame creation, argument binding, scope setup)
- Recursive traversal compounds this overhead across potentially deep AST trees
- Iterative approach uses a simple list for the stack, avoiding repeated function call overhead
- The `reversed()` call ensures children are processed in the same order as recursive traversal, preserving correctness
**3. Performance Characteristics**
Based on annotated tests:
- **Large method bodies** (500+ lines): 23.8% faster - most benefit from reduced recursion overhead
- **Many test methods** (100 methods): 9.2% faster - cumulative savings across many traversals
- **Simple cases**: 2-5% faster - overhead reduction still measurable
- **Empty/no-match cases**: Minor regression (8-9% slower) due to negligible baseline times (12-40μs)
## Impact on Workloads
The function references show `_add_timing_instrumentation` is called from test instrumentation code. This optimization particularly benefits:
- **Java projects with large test suites** containing many `@Test` methods
- **Complex test methods** with deep AST structures and multiple method invocations
- **Batch instrumentation operations** where the function is called repeatedly
The iterative approach scales better than recursion as AST depth and method count increase, making it especially valuable for large Java codebases where instrumentation is applied across hundreds of test methods.1 parent b42d97a commit 7cd5cda
1 file changed
Lines changed: 24 additions & 16 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
727 | 727 | | |
728 | 728 | | |
729 | 729 | | |
730 | | - | |
731 | | - | |
732 | | - | |
733 | | - | |
734 | | - | |
735 | | - | |
| 730 | + | |
| 731 | + | |
| 732 | + | |
| 733 | + | |
| 734 | + | |
| 735 | + | |
| 736 | + | |
| 737 | + | |
| 738 | + | |
| 739 | + | |
736 | 740 | | |
737 | 741 | | |
738 | | - | |
739 | | - | |
740 | | - | |
741 | | - | |
742 | | - | |
743 | | - | |
744 | | - | |
745 | | - | |
746 | | - | |
747 | | - | |
| 742 | + | |
| 743 | + | |
| 744 | + | |
| 745 | + | |
| 746 | + | |
| 747 | + | |
| 748 | + | |
| 749 | + | |
| 750 | + | |
| 751 | + | |
| 752 | + | |
| 753 | + | |
| 754 | + | |
748 | 755 | | |
749 | 756 | | |
750 | 757 | | |
| |||
842 | 849 | | |
843 | 850 | | |
844 | 851 | | |
| 852 | + | |
845 | 853 | | |
846 | 854 | | |
847 | 855 | | |
| |||
0 commit comments