-
Notifications
You must be signed in to change notification settings - Fork 21
[FIX] Prevent applying global assignments again when reverting the helpers #683
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
[FIX] Prevent applying global assignments again when reverting the helpers #683
Conversation
PR Reviewer Guide 🔍Here are some key observations to aid the review process:
|
PR Code Suggestions ✨Explore these optional code suggestions:
|
…licate-global-assignments-when-reverting-helpers
…/duplicate-global-assignments-when-reverting-helpers`) 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:** 1. **Eliminate duplicate parsing**: The original code parsed `src_module_code` and `dst_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. 2. **Reuse parsed modules**: Instead of re-parsing `dst_module_code` after modifications, the optimized version conditionally reuses the already-parsed `dst_module` when no global statements need insertion, avoiding unnecessary `cst.parse_module()` calls. 3. **Early termination**: Added an early return when `new_collector.assignments` is empty, avoiding the expensive `GlobalAssignmentTransformer` creation and visitation when there's nothing to transform. 4. **Minor optimization in uniqueness check**: Added a fast-path identity check (`stmt is existing_stmt`) before the expensive `deep_equals()` comparison, though this has minimal impact. **Performance impact by test case type:** - **Empty/minimal cases**: Show the highest gains (59-88% faster) due to early termination optimizations - **Standard cases**: Achieve consistent 20-30% improvements from reduced parsing - **Large-scale tests**: Benefit significantly (18-23% faster) as parsing overhead scales with code size 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()` and `module.visit()` operations.
⚡️ Codeflash found optimizations for this PR📄 18% (0.18x) speedup for
|
…25-08-25T18.50.33 ⚡️ Speed up function `add_global_assignments` by 18% in PR #683 (`fix/duplicate-global-assignments-when-reverting-helpers`)
This PR is now faster! 🚀 @mohammedahmed18 accepted my optimizations from: |
@@ -1783,7 +1782,6 @@ def new_function2(value): | |||
""" | |||
expected_code = """import numpy as np | |||
|
|||
print("Hello world") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we are only adding the unique global statements, since the exact print statement was in both original and optimized code, we should get only one statement in the final code not two
@@ -3453,3 +3447,157 @@ def hydrate_input_text_actions_with_field_names( | |||
main_file.unlink(missing_ok=True) | |||
|
|||
assert new_code == expected | |||
|
|||
def test_duplicate_global_assignments_when_reverting_helpers(): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the unstructured bug: mohammedahmed18/unstructured#1.
Since we revert helpers by applying the cached original file code back to the file, any existing global assignments in the cached code would get reapplied like here https://github.com/mohammedahmed18/unstructured/pull/1/files.
The fix was to skip adding global assignments during the revert process.
I don't think this is the best way for reverting helpers, so this is a temporary fix, until we have a better way for doing so