⚡️ Speed up method AsyncCallInstrumenter._process_test_function by 30% in PR #769 (clean-async-branch)
#771
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.