⚡️ Speed up method InjectPerfOnly.find_and_update_line_node by 22% in PR #867 (inspect-signature-issue)
#870
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 #867
If you approve this dependent PR, these changes will be merged into the original PR branch
inspect-signature-issue.📄 22% (0.22x) speedup for
InjectPerfOnly.find_and_update_line_nodeincodeflash/code_utils/instrument_existing_tests.py⏱️ Runtime :
2.01 milliseconds→1.64 milliseconds(best of113runs)📝 Explanation and details
The optimized code achieves a 22% speedup through two main optimizations that reduce overhead in AST traversal and attribute lookups:
1. Custom AST traversal replaces expensive
ast.walk()The original code uses
ast.walk()which creates recursive stack frames for every AST node. The optimized version implementsiter_ast_calls()- a manual iterative traversal that only visitsast.Callnodes using a single stack. This eliminates Python's recursion overhead and reduces the O(N) stack frame creation to a single stack operation.2. Reduced attribute lookups in hot paths
node_in_call_position(): Usesgetattr()with defaults to cache node attributes (node_lineno,node_end_lineno, etc.) instead of repeatedhasattr()+ attribute accessfind_and_update_line_node(): Hoists frequently-accessed object attributes (fn_obj.qualified_name,self.mode, etc.) to local variables before the loopcodeflash_loop_index,codeflash_cur,codeflash_con) instead of recreating them in each iterationPerformance characteristics:
ast.walk()The optimizations are most effective for complex test instrumentation scenarios with large AST trees or many call positions to check, which is typical in code analysis and transformation workflows.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-pr867-2025-11-01T00.02.02and push.