-
Notifications
You must be signed in to change notification settings - Fork 22
Import analyser fix #868
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?
Import analyser fix #868
Conversation
|
Codeflash Bot seems not to be a GitHub user. You need a GitHub account to be able to sign the CLA. If you have already a GitHub account, please add the email address used for this commit to your account. You have signed the CLA already but the status is still pending? Let us recheck it. |
PR Reviewer Guide 🔍Here are some key observations to aid the review process:
|
PR Code Suggestions ✨Explore these optional code suggestions:
|
The optimized code achieves a 38% speedup by eliminating expensive repeated string operations and set iterations within the hot path of `visit_Attribute()`.
**Key optimizations:**
1. **Precomputed lookup structures**: During initialization, the code now builds three efficient lookup structures:
- `_dot_methods`: Maps method names to sets of possible class names (e.g., "my_method" → {"MyClass", "OtherClass"})
- `_class_method_to_target`: Maps (class, method) tuples to full target names for O(1) reconstruction
- These replace the expensive loop that called `target_func.rsplit(".", 1)` on every function name for every attribute node
2. **Eliminated expensive loops**: The original code had nested loops iterating through all `function_names_to_find` for each attribute access. The optimized version uses fast hash table lookups (`self._dot_methods.get(node_attr)`) followed by set membership tests.
3. **Reduced attribute access overhead**: Local variables `node_value` and `node_attr` cache the attribute lookups to avoid repeated property access.
**Performance impact by test case type:**
- **Large alias mappings**: Up to 985% faster (23.4μs → 2.15μs) - most dramatic improvement when many aliases need checking
- **Large instance mappings**: 342% faster (9.35μs → 2.11μs) - significant gains with many instance variables
- **Class method access**: 24-27% faster - consistent improvement for dotted name resolution
- **Basic cases**: 7-15% faster - modest but consistent gains even for simple scenarios
The optimization is most effective for codebases with many qualified names (e.g., "Class.method" patterns) and particularly shines when the analyzer needs to check large sets of potential matches, which is common in real-world code discovery scenarios.
⚡️ Codeflash found optimizations for this PR📄 38% (0.38x) speedup for
|
…25-10-31T21.36.53 ⚡️ Speed up method `ImportAnalyzer.visit_Attribute` by 38% in PR #868 (`import-analyser-fix`)
|
This PR is now faster! 🚀 @misrasaurabh1 accepted my optimizations from: |
changing from individual tests to test files
User description
just match class name when method is not imported
for example
from code_to_optimize.topological_sort import Graph # topologicalSort not importedPR Type
Bug fix, Tests
Description
Match class-only imports to methods
Avoid missing class.method targets
Update E2E to assert test count
Diagram Walkthrough
File Walkthrough
discover_unit_tests.py
Fallback: match class import to class.methodcodeflash/discovery/discover_unit_tests.py
end_to_end_test_topological_sort_worktree.py
E2E: assert single discovered unit testtests/scripts/end_to_end_test_topological_sort_worktree.py