You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
⚡️ Speed up function get_first_top_level_function_or_method_ast by 17% in PR #678 (standalone-fto-async)
The optimized code achieves a **16% speedup** through several targeted micro-optimizations that reduce overhead in the tight loops that traverse AST nodes:
**Key Optimizations:**
1. **Local Variable Bindings**: Assigns `ast.iter_child_nodes` and the type tuple to local variables, eliminating repeated attribute lookups during iteration. The profiler shows this reduces the per-hit cost of the main loop from 1560.1ns to 1530.6ns.
2. **Restructured Type Checking**: Splits the combined `isinstance(child, object_type) and child.name == object_name` check into separate conditions. This allows early exit after the type check fails and uses `getattr(child, "name", None)` for safer attribute access, reducing the attribute lookup overhead shown in the profiler (from 403.8ns to 308ns per hit).
3. **Optimized Control Flow**: Changes the nested if-statements to `elif` structure, reducing redundant type checks. The `isinstance(child, fn_type_tuple)` check now only runs when needed, improving branch prediction.
4. **Direct Parent Access**: Caches `parents[0]` as `parent0` to avoid repeated list indexing, though this has minimal impact on the overall performance.
**Performance Impact by Test Type:**
- **Large-scale tests** (500+ functions/classes): Benefit most from reduced per-node overhead in deep traversals
- **Basic cases**: See consistent but smaller improvements due to fewer nodes processed
- **Edge cases**: Minimal impact since they often involve early returns or empty searches
The optimizations are most effective for codebases with complex AST structures where the functions traverse many nodes, making the micro-optimizations compound significantly.
0 commit comments