⚡️ Speed up method AsyncCallInstrumenter._process_test_function by 30% in PR #769 (clean-async-branch)#771
Closed
codeflash-ai[bot] wants to merge 1 commit into
Conversation
The optimized code achieves a 29% speedup through three key optimizations: **1. Replaced `ast.walk()` with manual stack traversal in `_instrument_statement()`** The original code used `ast.walk()` which creates a generator and recursively yields nodes. The optimized version uses an explicit stack with `ast.iter_child_nodes()`, eliminating generator overhead. This is the primary performance gain, as shown in the line profiler where `ast.walk()` took 81.9% of execution time in the original vs the new manual traversal being more efficient. **2. Optimized timeout decorator check with early exit** Instead of using `any()` with a generator expression that always evaluates all decorators, the optimized version uses a manual loop with `break` when the timeout decorator is found. This avoids unnecessary iterations when the decorator is found early, particularly beneficial for unittest frameworks. **3. Minor micro-optimizations** - Cached `self.async_call_counter` to a local variable to reduce attribute lookups - Replaced `hasattr(stmt, "lineno")` with `getattr(stmt, "lineno", 1)` to avoid double attribute access - Cached `node.decorator_list` reference to avoid repeated attribute access **Performance characteristics by test type:** - **Large-scale tests** (500+ async calls): The stack-based traversal shows significant gains due to reduced generator overhead - **unittest framework tests**: Early exit optimization provides 33-99% speedup when timeout decorators are found quickly - **Mixed target/non-target calls**: Manual traversal avoids unnecessary deep walks through non-matching nodes - **Small functions**: Minor but consistent 10-25% improvements from micro-optimizations The optimizations are most effective for codebases with many async calls or complex AST structures where the reduced generator overhead and early exits provide compound benefits.
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 #769
If you approve this dependent PR, these changes will be merged into the original PR branch
clean-async-branch.📄 30% (0.30x) speedup for
AsyncCallInstrumenter._process_test_functionincodeflash/code_utils/instrument_existing_tests.py⏱️ Runtime :
2.08 milliseconds→1.61 milliseconds(best of14runs)📝 Explanation and details
The optimized code achieves a 29% speedup through three key optimizations:
1. Replaced
ast.walk()with manual stack traversal in_instrument_statement()The original code used
ast.walk()which creates a generator and recursively yields nodes. The optimized version uses an explicit stack withast.iter_child_nodes(), eliminating generator overhead. This is the primary performance gain, as shown in the line profiler whereast.walk()took 81.9% of execution time in the original vs the new manual traversal being more efficient.2. Optimized timeout decorator check with early exit
Instead of using
any()with a generator expression that always evaluates all decorators, the optimized version uses a manual loop withbreakwhen the timeout decorator is found. This avoids unnecessary iterations when the decorator is found early, particularly beneficial for unittest frameworks.3. Minor micro-optimizations
self.async_call_counterto a local variable to reduce attribute lookupshasattr(stmt, "lineno")withgetattr(stmt, "lineno", 1)to avoid double attribute accessnode.decorator_listreference to avoid repeated attribute accessPerformance characteristics by test type:
The optimizations are most effective for codebases with many async calls or complex AST structures where the reduced generator overhead and early exits provide compound benefits.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-pr769-2025-09-26T23.14.56and push.