⚡️ Speed up function get_first_top_level_function_or_method_ast by 17% in PR #678 (standalone-fto-async)
#702
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
⚡️ This pull request contains optimizations for PR #678
If you approve this dependent PR, these changes will be merged into the original PR branch
standalone-fto-async.📄 17% (0.17x) speedup for
get_first_top_level_function_or_method_astincodeflash/code_utils/static_analysis.py⏱️ Runtime :
390 microseconds→334 microseconds(best of59runs)📝 Explanation and details
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:
Local Variable Bindings: Assigns
ast.iter_child_nodesand 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.Restructured Type Checking: Splits the combined
isinstance(child, object_type) and child.name == object_namecheck into separate conditions. This allows early exit after the type check fails and usesgetattr(child, "name", None)for safer attribute access, reducing the attribute lookup overhead shown in the profiler (from 403.8ns to 308ns per hit).Optimized Control Flow: Changes the nested if-statements to
elifstructure, reducing redundant type checks. Theisinstance(child, fn_type_tuple)check now only runs when needed, improving branch prediction.Direct Parent Access: Caches
parents[0]asparent0to avoid repeated list indexing, though this has minimal impact on the overall performance.Performance Impact by Test Type:
The optimizations are most effective for codebases with complex AST structures where the functions traverse many nodes, making the micro-optimizations compound significantly.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-pr678-2025-09-02T17.37.12and push.