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
⚡️ Speed up method ImportAnalyzer.visit_ImportFrom by 31% in PR #310 (test-filter-cleanup)
Here is an optimized version of your program. The main improvements are.
- Replace growing of `imported_modules` and `wildcard_modules` with faster local variables and reduced set insertion calls (avoid unnecessary growth).
- Check `self.found_any_target_function` immediately after mutating it, to avoid executing unnecessary lines.
- Use **early exits** aggressively (via return or break) to reduce the number of instructions and comparisons per `visit_ImportFrom`.
- Cache attributes/locals where appropriate to reduce attribute lookup cost inside loops.
- Use tuple membership check for dynamic import detection to avoid repeated string comparisons.
- Reduce the number of repeated dictionary lookups for `alias.name` and `alias.asname`.
**Summary of optimizations:**
- Minimize attribute lookups in tight loop (`target_functions`, `imported_modules`, `wildcard_modules` as locals).
- Use `continue` instead of `else` for `*` import check for clearer fast path.
- Avoid extra qualified name computation if already found.
- Remove comments only if the corresponding lines were changed for clarity.
- Fully preserve functional behavior and interface.
This will give a measurable speedup especially for large numbers of imports and in large ASTs.
0 commit comments