⚡️ Speed up method AsyncCallInstrumenter.visit_AsyncFunctionDef by 50% in PR #739 (get-throughput-from-output)#749
Closed
codeflash-ai[bot] wants to merge 1 commit into
Conversation
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:** 1. **Custom AST traversal replaces `ast.walk()`**: The original code used `ast.walk(stmt)` which visits *every* node in the AST subtree. The optimized version uses a manual stack-based traversal that only looks for `ast.Await` nodes, significantly reducing the number of nodes examined. 2. **Early termination**: Once an `ast.Await` node matching the target criteria is found, the search immediately breaks and returns, avoiding unnecessary traversal of remaining nodes. 3. **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 statements - The optimized version only examines nodes that could potentially contain `ast.Await` expressions, dramatically reducing the search space - For large test functions with many statements (as shown in the annotated tests), this optimization scales particularly well - the 500+ await call test cases show **50-53% speedup** The optimization is most effective for test cases with: - Large numbers of async function calls (50%+ improvement) - Complex nested structures with few actual target calls (40%+ improvement) - Mixed await patterns where only some calls need instrumentation (35%+ improvement)
Contributor
Author
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.