⚡️ Speed up method ImportAnalyzer.visit_Attribute by 38% in PR #868 (import-analyser-fix)
#869
+40
−31
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 #868
If you approve this dependent PR, these changes will be merged into the original PR branch
import-analyser-fix.📄 38% (0.38x) speedup for
ImportAnalyzer.visit_Attributeincodeflash/discovery/discover_unit_tests.py⏱️ Runtime :
114 microseconds→82.6 microseconds(best of250runs)📝 Explanation and details
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:
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) reconstructiontarget_func.rsplit(".", 1)on every function name for every attribute nodeEliminated expensive loops: The original code had nested loops iterating through all
function_names_to_findfor each attribute access. The optimized version uses fast hash table lookups (self._dot_methods.get(node_attr)) followed by set membership tests.Reduced attribute access overhead: Local variables
node_valueandnode_attrcache the attribute lookups to avoid repeated property access.Performance impact by test case type:
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.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-pr868-2025-10-31T21.36.53and push.