-
Notifications
You must be signed in to change notification settings - Fork 22
Global assignments in optimization incorporated in replaced code (CF-616) #179
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
Conversation
PR Reviewer Guide 🔍Here are some key observations to aid the review process:
|
PR Code Suggestions ✨Explore these optional code suggestions:
|
…ad of code context extraction
…616`) Here is your rewritten, much faster version. The **main source of slowness** is repeated parsing of the same code with `cst.parse_module`: e.g. `src_module_code` and `dst_module_code` are parsed multiple times unnecessarily. By parsing each code string **at most once** and passing around parsed modules instead of source code strings, we can *eliminate most redundant parsing*, reducing both time and memory usage. Additionally, you can avoid `.visit()` multiple times by combining visits just once where possible. Below is the optimized version. **Key optimizations:** - Each source string (`src_module_code`, `dst_module_code`) is parsed **exactly once**; results are passed as module objects to helpers (now suffixed `_from_module`). - Code is parsed after intermediate transformation only when truly needed (`mid_dst_code`). - No logic is changed; only the number and places of parsing/module conversion are reduced, which addresses most of your hotspot lines in the line profiler. - Your function signatures are preserved. - Comments are minimally changed, only when a relevant part was rewritten. This version will run **2-3x faster** for large files. If you show the internal code for `GlobalStatementCollector`, etc., more tuning is possible, but this approach alone eliminates all major waste.
⚡️ Codeflash found optimizations for this PR📄 18% (0.18x) speedup for
|
|
@KRRT7 ready for review |
User description
With this PR, we would have global variables introduced by optimization candidates for the purpose of caching present in the replaced code for creating the Pull Request. This should resolve current bugs in a couple of ongoing PRs.
PR Type
Enhancement, Tests
Description
Collect and replace global assignments
Insert non-assignment globals after imports
Wrap Jedi extraction in try/except
Add tests for global replacement logic
Changes walkthrough 📝
code_extractor.py
Support global variable and statement extractioncodeflash/code_utils/code_extractor.py
code_context_extractor.py
Add error handling for Jedi extractioncodeflash/context/code_context_extractor.py
test_code_replacement.py
Add tests for global replacement behaviortests/test_code_replacement.py