Commit e5b3438
authored
The optimization significantly improves performance by **eliminating redundant AST traversals** in the `visit_ClassDef` method.
**Key optimization:** Replace `ast.walk(node)` with direct iteration over `node.body`. The original code uses `ast.walk()` which performs a deep recursive traversal of the entire AST subtree, visiting every nested node including those inside method bodies, nested classes, and compound statements. This creates O(n²) complexity when combined with the subsequent `visit_FunctionDef` calls.
**Why this works:** The method only needs to find direct child nodes that are `FunctionDef` or `AsyncFunctionDef` to process them. Direct iteration over `node.body` achieves the same result in O(n) time since it only examines immediate children of the class.
**Performance impact:** The line profiler shows the critical bottleneck - the `ast.walk()` call took 88.2% of total execution time (27ms out of 30.6ms) in the original version. The optimized version reduces this to just 10.3% (207μs out of 2ms), achieving a **2017% speedup**.
**Optimization effectiveness:** This change is particularly beneficial for large test classes with many methods (as shown in the annotated tests achieving 800-2500% speedups), where the unnecessary deep traversal of method bodies becomes increasingly expensive. The optimization maintains identical behavior while dramatically reducing computational overhead for AST processing workflows.
1 parent c4e3e00 commit e5b3438
1 file changed
+11
-11
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
222 | 222 | | |
223 | 223 | | |
224 | 224 | | |
225 | | - | |
| 225 | + | |
| 226 | + | |
226 | 227 | | |
227 | 228 | | |
228 | 229 | | |
| |||
269 | 270 | | |
270 | 271 | | |
271 | 272 | | |
| 273 | + | |
272 | 274 | | |
273 | 275 | | |
274 | 276 | | |
275 | 277 | | |
276 | | - | |
277 | | - | |
278 | | - | |
279 | | - | |
280 | | - | |
281 | | - | |
282 | | - | |
283 | | - | |
284 | | - | |
285 | | - | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
286 | 286 | | |
287 | 287 | | |
288 | 288 | | |
| |||
0 commit comments