⚡️ Speed up function add_global_assignments
by 18% in PR #683 (fix/duplicate-global-assignments-when-reverting-helpers
)
#686
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 #683
If you approve this dependent PR, these changes will be merged into the original PR branch
fix/duplicate-global-assignments-when-reverting-helpers
.📄 18% (0.18x) speedup for
add_global_assignments
incodeflash/code_utils/code_extractor.py
⏱️ Runtime :
1.23 seconds
→1.05 seconds
(best of9
runs)📝 Explanation and details
The optimized code achieves a 17% speedup by eliminating redundant CST parsing operations, which are the most expensive parts of the function according to the line profiler.
Key optimizations:
Eliminate duplicate parsing: The original code parsed
src_module_code
anddst_module_code
multiple times. The optimized version introduces_extract_global_statements_once()
that parses each module only once and reuses the parsed CST objects throughout the function.Reuse parsed modules: Instead of re-parsing
dst_module_code
after modifications, the optimized version conditionally reuses the already-parseddst_module
when no global statements need insertion, avoiding unnecessarycst.parse_module()
calls.Early termination: Added an early return when
new_collector.assignments
is empty, avoiding the expensiveGlobalAssignmentTransformer
creation and visitation when there's nothing to transform.Minor optimization in uniqueness check: Added a fast-path identity check (
stmt is existing_stmt
) before the expensivedeep_equals()
comparison, though this has minimal impact.Performance impact by test case type:
The optimization is most effective for workloads with moderate to large code files where CST parsing dominates the runtime, as evidenced by the original profiler showing 70%+ of time spent in
cst.parse_module()
andmodule.visit()
operations.✅ Correctness verification report:
⚙️ Existing Unit Tests and Runtime
test_code_context_extractor.py::test_circular_deps
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-pr683-2025-08-25T18.50.33
and push.