⚡️ Speed up function create_wrapper_function by 14% in PR #617 (alpha-async)
#618
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 #617
If you approve this dependent PR, these changes will be merged into the original PR branch
alpha-async.📄 14% (0.14x) speedup for
create_wrapper_functionincodeflash/code_utils/instrument_existing_tests.py⏱️ Runtime :
1.93 milliseconds→1.70 milliseconds(best of58runs)📝 Explanation and details
The optimized code achieves a 13% speedup by eliminating redundant AST node creation through aggressive caching and reuse.
What specific optimizations were applied:
Pre-constructed AST node cache: Instead of creating new
ast.Nameandast.Attributeobjects repeatedly (the original created hundreds of identical nodes), the optimized version creates each unique node once and reuses it. For example,n_test_module_name_L = name("test_module_name", LOAD)is created once and reused everywhere.Constant node reuse: Common constants like
ast.Constant(value=":")are created once asc_colonand reused throughout, eliminating duplicate allocations.Factory functions for complex structures:
JoinedStrconstructions are moved into reusable factory functions likejoinedstr_test_id(), reducing code duplication and enabling better optimization.Why this leads to speedup:
ast.Name()hundreds of times, it's called once per unique identifier.Test case performance patterns:
The optimization shows consistent 15-20% improvements across most test cases, with particularly strong gains in tests that exercise the full AST construction pipeline (like
test_assigns_test_idshowing 19% improvement). The speedup is most pronounced for tests that trigger the complexJoinedStrconstructions, which benefited most from the caching strategy.This optimization is especially effective for workloads that call
create_wrapper_functionrepeatedly, as the AST node reuse compound the benefits over multiple invocations.✅ Correctness verification report:
⚙️ Existing Unit Tests and Runtime
test_async_instrumentation.py::test_async_wrapper_preserves_return_valuetest_async_instrumentation.py::test_mixed_sync_async_instrumentationtest_async_instrumentation.py::test_wrapper_function_includes_async_check🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-pr617-2025-08-06T00.05.08and push.