⚡️ Speed up method AsyncCallInstrumenter.visit_AsyncFunctionDef by 50% in PR #739 (get-throughput-from-output)
#749
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 #739
If you approve this dependent PR, these changes will be merged into the original PR branch
get-throughput-from-output.📄 50% (0.50x) speedup for
AsyncCallInstrumenter.visit_AsyncFunctionDefincodeflash/code_utils/instrument_existing_tests.py⏱️ Runtime :
6.85 milliseconds→4.56 milliseconds(best of103runs)📝 Explanation and details
The optimized code achieves a 50% speedup by replacing the expensive
ast.walk()traversal with a targeted stack-based search in the new_instrument_statement_fast()method.Key optimizations:
Custom AST traversal replaces
ast.walk(): The original code usedast.walk(stmt)which visits every node in the AST subtree. The optimized version uses a manual stack-based traversal that only looks forast.Awaitnodes, significantly reducing the number of nodes examined.Early termination: Once an
ast.Awaitnode matching the target criteria is found, the search immediately breaks and returns, avoiding unnecessary traversal of remaining nodes.Optimized decorator checking: The
any()generator expression is replaced with a simple for-loop that can exit early when a timeout decorator is found, though this provides minimal gains compared to the AST optimization.Why this works so well:
ast.walk()performs a breadth-first traversal of all nodes in the AST subtree, which can be hundreds of nodes for complex statementsast.Awaitexpressions, dramatically reducing the search spaceThe optimization is most effective for test cases with:
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-pr739-2025-09-23T04.24.41and push.