Commit 20d7221
authored
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.
1 parent dae4afc commit 20d7221
1 file changed
+28
-24
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
178 | 178 | | |
179 | 179 | | |
180 | 180 | | |
181 | | - | |
| 181 | + | |
182 | 182 | | |
183 | 183 | | |
184 | | - | |
185 | | - | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
186 | 188 | | |
187 | 189 | | |
188 | | - | |
189 | | - | |
190 | | - | |
191 | | - | |
192 | | - | |
193 | | - | |
194 | | - | |
195 | | - | |
196 | | - | |
197 | | - | |
198 | | - | |
199 | | - | |
200 | | - | |
201 | | - | |
202 | | - | |
203 | | - | |
204 | | - | |
205 | | - | |
206 | | - | |
207 | | - | |
208 | | - | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
209 | 213 | | |
210 | 214 | | |
211 | 215 | | |
| |||
0 commit comments