⚡️ Speed up method CommentMapper.visit_FunctionDef
by 84% in PR #687 (granular-async-instrumentation
)
#709
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 #687
If you approve this dependent PR, these changes will be merged into the original PR branch
granular-async-instrumentation
.📄 84% (0.84x) speedup for
CommentMapper.visit_FunctionDef
incodeflash/code_utils/edit_generated_tests.py
⏱️ Runtime :
3.03 milliseconds
→1.65 milliseconds
(best of148
runs)📝 Explanation and details
The optimization replaces the expensive
ast.walk()
call with a targeted node traversal that only checks the immediate statement and its direct body children.Key change: Instead of
ast.walk(compound_line_node)
which recursively traverses the entire AST subtree, the optimized code creates a focused list:This dramatically reduces the number of nodes processed in the inner loop. The line profiler shows
ast.walk()
was the major bottleneck (46.2% of total time, 8.23ms), while the optimized version's equivalent loop takes only 1.9% of total time (180μs).Why this works: The code only needs to check statements at the current level and one level deep (direct children in compound statement bodies like
for
,if
,while
,with
). The originalast.walk()
was doing unnecessary deep traversal of nested structures.Performance impact: The optimization is most effective for test cases with compound statements (for/while/if/with blocks) containing multiple nested nodes, showing 73-156% speedups in those scenarios. Simple statement functions see smaller but consistent 1-3% improvements due to reduced overhead.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-pr687-2025-09-03T05.27.10
and push.