Commit 91d8b97
committed
Defer the eager scipy.sparse and scipy.spatial imports (~280 ms)
`import autoarray` drops from 464.4 ms to 183.7 ms (medians of 15 runs,
Python 3.13, dev extras) — a 281 ms saving.
The filed task named `derivative_util.py:30`'s module-scope
`from scipy.sparse import csr_matrix` as the target, on the evidence that
`scipy.sparse` costs ~0.11 s of every import. Deferring it changed nothing,
because `scipy.sparse` was never being imported from there:
autoarray/__init__.py:80
-> inversion/mesh/mesh_geometry/delaunay.py:2 import scipy.spatial
-> scipy/spatial/__init__.py:111 from ._kdtree import *
-> scipy/spatial/_kdtree.py:4 from ._ckdtree import cKDTree
`scipy.spatial` (134 ms) pulls `scipy.sparse` (154 ms) in transitively, so the
csr_matrix import was riding on a subtree that was already paid for. Deferring
`scipy.spatial` as well is what actually removes both, and is required to meet
the task's own acceptance criterion.
Changes:
- `inversion/mesh/mesh_geometry/delaunay.py` — drop the module-scope
`import scipy.spatial`. Two of its three use sites already had local
imports; only `voronoi_neighbors_from` needed one added, so this finishes a
deferral that had been started and left half-done.
- `operators/derivative_util.py`, `operators/coarse_interp_util.py` — move
`from scipy.sparse import csr_matrix` into the four functions that use it.
No longer load-bearing for the import time on its own, but it keeps
`scipy.sparse` off the import path independently of what `scipy.spatial`
happens to pull in.
Every use site is inside a function, so a plain local import suffices — no
module-level cache as in `transformer.py:_load_nufftax()`, which needed one
only because unpickled instances in multiprocessing workers never re-run
`__init__`. Function-local imports run on every call and hit `sys.modules`
after the first.
Verified: `python -X importtime -c "import autoarray"` shows neither
`scipy.sparse` nor `scipy.spatial`. Suites green — autoarray 1179 passed,
autogalaxy 1103 passed / 1 skipped, autolens 532 passed / 1 skipped.1 parent c330e3c commit 91d8b97
4 files changed
Lines changed: 11 additions & 5 deletions
File tree
- autoarray
- inversion/mesh/mesh_geometry
- operators
- test_autoarray/structures/arrays/files/array/output_test
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | | - | |
3 | 2 | | |
4 | 3 | | |
5 | 4 | | |
| |||
147 | 146 | | |
148 | 147 | | |
149 | 148 | | |
| 149 | + | |
150 | 150 | | |
151 | 151 | | |
152 | 152 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
17 | 17 | | |
18 | 18 | | |
19 | 19 | | |
20 | | - | |
21 | 20 | | |
22 | 21 | | |
23 | 22 | | |
| |||
242 | 241 | | |
243 | 242 | | |
244 | 243 | | |
| 244 | + | |
| 245 | + | |
245 | 246 | | |
246 | 247 | | |
247 | 248 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
27 | 27 | | |
28 | 28 | | |
29 | 29 | | |
30 | | - | |
31 | 30 | | |
32 | 31 | | |
33 | 32 | | |
| |||
297 | 296 | | |
298 | 297 | | |
299 | 298 | | |
| 299 | + | |
| 300 | + | |
300 | 301 | | |
301 | | - | |
302 | | - | |
| 302 | + | |
| 303 | + | |
303 | 304 | | |
304 | 305 | | |
305 | 306 | | |
| |||
443 | 444 | | |
444 | 445 | | |
445 | 446 | | |
| 447 | + | |
| 448 | + | |
446 | 449 | | |
447 | 450 | | |
448 | 451 | | |
| |||
574 | 577 | | |
575 | 578 | | |
576 | 579 | | |
| 580 | + | |
| 581 | + | |
577 | 582 | | |
578 | 583 | | |
579 | 584 | | |
| |||
Binary file not shown.
0 commit comments