You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
0 commit comments