⚡️ Speed up method CommentMapper.visit_AsyncFunctionDef by 399% in PR #769 (clean-async-branch)
#777
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.📄 399% (3.99x) speedup for
CommentMapper.visit_AsyncFunctionDefincodeflash/code_utils/edit_generated_tests.py⏱️ Runtime :
1.22 milliseconds→244 microseconds(best of178runs)📝 Explanation and details
The optimization achieves a 398% speedup by reducing repeated attribute lookups and string operations that were happening in tight loops.
Key optimizations applied:
Cached repeated attribute lookups: Instead of accessing
self.context_stack,self.original_runtimes,self.optimized_runtimes, andself.get_commentrepeatedly in loops, these are cached as local variables at the start of the method. This eliminates hundreds of attribute lookups during execution.Pre-computed string concatenation: The original code was rebuilding
test_qualified_name + "#" + str(self.abs_path)on every function call. The optimized version computeskey_baseonce and reuses it, avoiding repeated string joins andstr()calls.Optimized loop structure: Replaced the
while j >= 0loop withfor j in range(compound_body_len - 1, -1, -1)which has better performance characteristics and eliminates repeatedlen()calls.Streamlined getattr usage: Instead of calling
getattr(compound_line_node, "body", [])and then extending a list, the code now checks for the presence of a body attribute once and conditionally adds it, reducing function call overhead.F-string optimization: Replaced string concatenation with f-strings (
f"{i}_{j}"instead ofstr(i) + "_" + str(j)) which are more efficient in Python.The line profiler shows the dramatic impact - the most expensive operations in the original (like
self.get_comment()calls taking 23.9% and 18.8% of total time) now execute much faster due to cached attribute lookups. The optimizations are particularly effective for large-scale test cases with many statements or nested compound structures, showing 400%+ speedups in functions with 100+ statements.✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-pr769-2025-09-27T01.01.48and push.