Commit ea151d7
authored
Optimize JavaAnalyzer.find_methods
The optimized code achieves an **11% runtime improvement** (24.5ms → 22.0ms) by eliminating recursive function call overhead through two key optimizations:
## Primary Optimization: Iterative Tree Traversal
The core improvement replaces recursive calls to `_walk_tree_for_methods` with an explicit stack-based iteration. In Python, each recursive call incurs significant overhead from:
- Stack frame creation and teardown
- Parameter passing (6 parameters per call)
- Return address management
The profiler data confirms this: the original code spent 24.5% of time in recursive call setup (lines 39338 hits at 816.7ns per hit), while the optimized version eliminates this entirely by using a stack data structure.
The iterative approach processes nodes in the same depth-first, left-to-right order (by reversing children before pushing to stack) but replaces ~19,726 recursive function calls with simple stack operations. This is particularly effective for Java code analysis where the AST can have deep nesting (nested classes, methods, etc.).
## Secondary Optimization: Type Declaration Tuple Caching
Moving `type_declarations = ("class_declaration", "interface_declaration", "enum_declaration")` from a local variable allocated on every call (19,726 times) to a single instance attribute `self._type_declarations` eliminates 19,726 tuple allocations. The profiler shows this saved 3.3% of execution time in the original version.
## Performance Characteristics
The optimization excels on test cases with:
- **Many methods** (100+ methods): 11.6-14.6% speedup as recursive overhead compounds
- **Deep nesting** (nested classes): 8.85% speedup by avoiding deep call stacks
- **Large files with filtering**: 11-12% speedup as the stack approach handles conditional logic efficiently
- **Mixed interfaces/classes**: 13.2% speedup due to reduced overhead when tracking type context
The optimization maintains identical correctness across all test cases, preserving method discovery, filtering behavior, line numbers, class tracking, and return types.1 parent e86f21e commit ea151d7
1 file changed
Lines changed: 36 additions & 30 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
113 | 113 | | |
114 | 114 | | |
115 | 115 | | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
116 | 119 | | |
117 | 120 | | |
118 | 121 | | |
| |||
165 | 168 | | |
166 | 169 | | |
167 | 170 | | |
| 171 | + | |
| 172 | + | |
168 | 173 | | |
169 | | - | |
| 174 | + | |
170 | 175 | | |
171 | 176 | | |
172 | 177 | | |
| |||
186 | 191 | | |
187 | 192 | | |
188 | 193 | | |
189 | | - | |
190 | | - | |
191 | | - | |
192 | | - | |
193 | | - | |
194 | | - | |
195 | | - | |
196 | | - | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
197 | 208 | | |
198 | | - | |
199 | | - | |
| 209 | + | |
| 210 | + | |
200 | 211 | | |
201 | | - | |
202 | | - | |
203 | | - | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
204 | 215 | | |
205 | | - | |
206 | | - | |
| 216 | + | |
| 217 | + | |
207 | 218 | | |
208 | | - | |
209 | | - | |
| 219 | + | |
| 220 | + | |
210 | 221 | | |
211 | | - | |
212 | | - | |
| 222 | + | |
| 223 | + | |
213 | 224 | | |
214 | | - | |
215 | | - | |
216 | | - | |
217 | | - | |
218 | | - | |
219 | | - | |
220 | | - | |
221 | | - | |
222 | | - | |
223 | | - | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
224 | 230 | | |
225 | 231 | | |
226 | 232 | | |
| |||
0 commit comments