Commit 36253f5
authored
Here is an optimized version of your program, focusing on the major bottleneck seen in the line profiler:
`stack.extend(ast.iter_child_nodes(node))` is taking **80%** of the total runtime.
### Main Optimization
- **Avoid repeated iterator creation:**
`ast.iter_child_nodes` creates a generator each time, and extending with a generator is slower than extending with a list due to type checks and resizing on the list.
Changing this to `stack.extend(list(ast.iter_child_nodes(node)))` is often faster for small lists (due to C-optimized list logic).
- **Pre-memoize field lookups:**
`ast.iter_child_nodes` re-inspects _fields each time. Since you only traverse AST (no node mutation), accessing `_fields` directly and using them is faster.
- **Better local variable usage:**
Move global lookups like `ast.Return` to locals for faster lookups.
- **Use `is` for type checks when possible:**
Since `ast` node classes are not subclassed, `type(node) is ast.Return` is a micro-optimization.
- **Micro-optimization:**
Replace `.extend()` with multiple `.append()` only if profiling (for very shallow trees), but since ASTs can be deep, bulk operation is preferred.
----
*.
### If you want highest speed, *completely bypass ast.iter_child_nodes* as below.
**This version skips all repeated lookups in ast.iter_child_nodes.**
----
### Summary of what changed.
- Pre-bind `ast.Return` and `ast.iter_child_nodes` to locals.
- Use `type(node) is ast.Return` instead of `isinstance`.
- Use `list(...)` inside `.extend()` for batch insertion.
- Optionally, custom child iteration for maximum speed (bypassing `iter_child_nodes`).
---
**You can choose either of the two optimized versions above depending on your use case.**
If you want only a drop-in fix, use the first rewrite. If you want _maximum speed_, use the custom field walker.
All existing comments are preserved.
1 parent edfc240 commit 36253f5
1 file changed
Lines changed: 11 additions & 3 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
599 | 599 | | |
600 | 600 | | |
601 | 601 | | |
602 | | - | |
603 | 602 | | |
| 603 | + | |
604 | 604 | | |
605 | 605 | | |
606 | | - | |
| 606 | + | |
607 | 607 | | |
608 | | - | |
| 608 | + | |
| 609 | + | |
| 610 | + | |
| 611 | + | |
| 612 | + | |
| 613 | + | |
| 614 | + | |
| 615 | + | |
| 616 | + | |
609 | 617 | | |
610 | 618 | | |
611 | 619 | | |
| |||
0 commit comments