-
Notifications
You must be signed in to change notification settings - Fork 5k
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
fix: Ensure importability of modules not included in __init__.py files #5965
Merged
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
dosubot
bot
added
the
size:XS
This PR changes 0-9 lines, ignoring generated files.
label
Jan 27, 2025
codeflash-ai bot
added a commit
that referenced
this pull request
Jan 27, 2025
…from-imports`) To optimize the program for both execution speed and memory usage, we can make some modifications. Specifically, we'll eliminate repetitive calls to `compile` and `exec` for `ClassDef`, `FunctionDef`, and `Assign` nodes. Instead, we'll compile all such nodes together and then execute them in one go. We will also handle imports more efficiently to avoid re-importing modules multiple times. Here's the rewritten code. ### Explanation. 1. **Avoid Re-importing**: The code now checks if the module or alias is already in `exec_globals` to avoid re-importing it. 2. **Batch Compilation and Execution**: All `ClassDef`, `FunctionDef`, and `Assign` nodes are collected in `exec_bodies`. They are then compiled and executed together in one call to `exec`, reducing the overhead. 3. **Using Warnings Context Manager**: Warnings for deprecations are handled using a context manager, ensuring that the warning filter is applied only for the scope needing it. These changes should help improve the overall runtime performance and memory efficiency of the function.
dosubot
bot
added
size:S
This PR changes 10-29 lines, ignoring generated files.
and removed
size:XS
This PR changes 0-9 lines, ignoring generated files.
labels
Jan 27, 2025
github-actions
bot
added
bug
Something isn't working
and removed
bug
Something isn't working
labels
Jan 27, 2025
codeflash-ai bot
added a commit
that referenced
this pull request
Jan 27, 2025
…from-imports`) To optimize the program, we can make a few improvements like reducing the number of compile and exec calls by batching statements together, which will help in reducing the overhead introduced by multiple invocations. Additionally, using defaultdict for storing the import statements may simplify the logic and enhance readability. Let's rewrite the code with these optimizations. Changes and optimizations. 1. **Batched Imports:** Grouping import and import-from statements separately to reduce redundancy. 2. **Batch Compilation:** Aggregated the handling of class, function, and assignment definitions into fewer compile and exec calls to limit overhead. 3. **Code Simplification:** Use defaultdict to store import and import-from statements for better readability and manageability. These changes aim to make the function more efficient by reducing repetitive operations and improving clarity.
CodSpeed Performance ReportMerging #5965 will degrade performances by 96.06%Comparing Summary
Benchmarks breakdown
|
italojohnny
approved these changes
Jan 27, 2025
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Enhance the import mechanism to allow modules that are not explicitly included in init.py files to be importable by attempting to access them as attributes first, and falling back to importing the full module path if necessary.