Commit 8ee82e5
committed
perf: defer Matplotlib import until something actually plots
`import spatialmath` unconditionally pulled in all of Matplotlib,
even for users who never plot anything: `spatialmath/base/__init__.py`
did `from spatialmath.base.animate import *` / `from
spatialmath.base.graphics import *` at package-import time, and those
two modules do `import matplotlib.pyplot`. `geom2d.py`, `geom3d.py`,
`spline.py`, and both `transforms2d.py`/`transforms3d.py` had their
own copies of the same pattern, some via a `try: import
matplotlib.pyplot ... except ImportError:` optional-dependency check
that still imported eagerly, just without crashing if it failed.
Measured on this machine: bare `import matplotlib` costs ~96ms,
`matplotlib.path` (needed structurally by Polygon2's geometry, not
just plotting) adds ~0ms on top of that, but `matplotlib.pyplot`
specifically (backend resolution + figure/state setup) adds another
~160ms. That pyplot cost is what this change removes from `import
spatialmath`.
Approach: don't touch matplotlib usage inside graphics.py/animate.py
at all (dozens of `plt.Axes`-style annotations in there - rewriting
those would be the disproportionate version of this fix). Instead,
defer the module import itself:
- spatialmath/base/__init__.py: replace the two blanket `import *`
lines with a PEP 562 module `__getattr__` that imports
animate.py/graphics.py lazily on first access of one of their
names (Animate, plot_box, tranimate, etc.) rather than
unconditionally at package import time.
- spatialmath/base/transforms2d.py, transforms3d.py: both had a
module-level `try: import matplotlib.pyplot ... except
ImportError: _matplotlib_exists = False` used purely to decide
whether to define trplot/trplot2/tranimate/tranimate2 at all.
Replaced with a cheap `importlib.util.find_spec("matplotlib")`
check (no real import), and moved the small number of actual
`plotvol2/3`, `axes_logic`, `Animate`/`Animate2` and `plt.show()`
call sites into the specific functions that use them - they were
already the only things those two files needed matplotlib for.
- spatialmath/spline.py, geom3d.py: same pattern, moved `import
matplotlib.pyplot as plt` from module top into the one or two
methods that actually plot.
- spatialmath/geom2d.py: nuance here - Polygon2.__init__ uses
`matplotlib.path.Path` for real geometry (point containment etc.),
and Polygon2.transformed() uses `matplotlib.transforms.Affine2D`
for the same reason, so both stay at module level (cheap anyway,
per the measurement above). Only `matplotlib.pyplot` itself, and
the `plot_ellipse` import (only used by Ellipse.plot), moved.
All of the above needed `from __future__ import annotations` added
where not already present, so a signature like `ax: Optional[plt.Axes]
= None` doesn't force `plt` to be a real bound name at function
*definition* time (which happens at module-import time, before the
deferred import ever runs) - annotations become lazy strings instead,
which type checkers still read fine. Matches the existing convention
in 8 other files in this codebase.
One real (if minor) bug this surfaced: tests/test_geom3d.py used
`plt.figure()` without importing matplotlib.pyplot itself - it was
only working because `from spatialmath.geom3d import *` used to leak
`plt` in as a wildcard-imported name. Fixed by importing it directly,
which is what the test should have done regardless of this change.
Verified:
- `import spatialmath` no longer puts `matplotlib.pyplot` in
sys.modules; `matplotlib` (the ~96ms base package, structurally
needed by Polygon2's geometry) still does.
- Wall-clock `import spatialmath`, same machine, same warm caches:
~670ms average on 3 runs before this change (upstream/master, via a
throwaway worktree), ~447ms average after.
- Full test suite green in both CI-like mode (`CI=true
MPLBACKEND=Agg`: 338 passed, 4 skipped) and a local-run simulation
(`MPLBACKEND=Agg`, `CI` unset): 332 passed (excluding two files that
depend on a separate, already-open PR for their own local-run
fixes, unrelated to this change).
- Manual smoke test: trplot, trplot2, Polygon2.plot/.animate/.contains,
Ellipse.plot, Line3.plot, Plane3.plot, BSplineSE3.visualize all still
produce real output under a forced Agg backend.
- `black --check` clean at the pinned 23.10.0.1 parent 8b83c1f commit 8ee82e5
7 files changed
Lines changed: 103 additions & 26 deletions
File tree
- spatialmath
- base
- tests
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
9 | 9 | | |
10 | 10 | | |
11 | 11 | | |
12 | | - | |
13 | | - | |
14 | 12 | | |
15 | 13 | | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
16 | 64 | | |
17 | 65 | | |
18 | 66 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
1 | 3 | | |
2 | 4 | | |
3 | 5 | | |
| |||
16 | 18 | | |
17 | 19 | | |
18 | 20 | | |
| 21 | + | |
| 22 | + | |
19 | 23 | | |
20 | 24 | | |
21 | | - | |
22 | | - | |
23 | | - | |
24 | | - | |
25 | | - | |
26 | | - | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
27 | 29 | | |
28 | 30 | | |
29 | 31 | | |
30 | 32 | | |
31 | 33 | | |
32 | 34 | | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
33 | 38 | | |
34 | 39 | | |
35 | 40 | | |
| |||
1227 | 1232 | | |
1228 | 1233 | | |
1229 | 1234 | | |
1230 | | - | |
1231 | | - | |
1232 | | - | |
1233 | | - | |
1234 | 1235 | | |
1235 | 1236 | | |
1236 | 1237 | | |
| |||
1485 | 1486 | | |
1486 | 1487 | | |
1487 | 1488 | | |
| 1489 | + | |
| 1490 | + | |
1488 | 1491 | | |
1489 | 1492 | | |
1490 | 1493 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
1 | 3 | | |
2 | 4 | | |
3 | 5 | | |
| |||
15 | 17 | | |
16 | 18 | | |
17 | 19 | | |
| 20 | + | |
18 | 21 | | |
19 | 22 | | |
| 23 | + | |
20 | 24 | | |
21 | 25 | | |
22 | 26 | | |
| |||
44 | 48 | | |
45 | 49 | | |
46 | 50 | | |
47 | | - | |
48 | | - | |
49 | 51 | | |
50 | 52 | | |
51 | 53 | | |
52 | 54 | | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
53 | 60 | | |
54 | 61 | | |
55 | 62 | | |
| |||
2910 | 2917 | | |
2911 | 2918 | | |
2912 | 2919 | | |
2913 | | - | |
2914 | | - | |
2915 | | - | |
2916 | | - | |
2917 | | - | |
2918 | | - | |
2919 | | - | |
| 2920 | + | |
| 2921 | + | |
| 2922 | + | |
| 2923 | + | |
2920 | 2924 | | |
2921 | 2925 | | |
2922 | 2926 | | |
| |||
3107 | 3111 | | |
3108 | 3112 | | |
3109 | 3113 | | |
| 3114 | + | |
| 3115 | + | |
3110 | 3116 | | |
3111 | 3117 | | |
3112 | 3118 | | |
| |||
3419 | 3425 | | |
3420 | 3426 | | |
3421 | 3427 | | |
| 3428 | + | |
| 3429 | + | |
3422 | 3430 | | |
3423 | 3431 | | |
3424 | 3432 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
9 | 9 | | |
10 | 10 | | |
11 | 11 | | |
12 | | - | |
| 12 | + | |
13 | 13 | | |
14 | 14 | | |
15 | 15 | | |
16 | 16 | | |
17 | 17 | | |
18 | 18 | | |
19 | 19 | | |
20 | | - | |
21 | 20 | | |
22 | 21 | | |
23 | 22 | | |
| |||
37 | 36 | | |
38 | 37 | | |
39 | 38 | | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
40 | 42 | | |
41 | 43 | | |
42 | 44 | | |
| |||
450 | 452 | | |
451 | 453 | | |
452 | 454 | | |
| 455 | + | |
| 456 | + | |
453 | 457 | | |
454 | 458 | | |
455 | 459 | | |
| |||
1063 | 1067 | | |
1064 | 1068 | | |
1065 | 1069 | | |
| 1070 | + | |
| 1071 | + | |
1066 | 1072 | | |
1067 | 1073 | | |
1068 | 1074 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
6 | 6 | | |
7 | 7 | | |
8 | 8 | | |
9 | | - | |
| 9 | + | |
10 | 10 | | |
11 | 11 | | |
12 | 12 | | |
13 | 13 | | |
14 | 14 | | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
15 | 18 | | |
16 | 19 | | |
17 | 20 | | |
| |||
1232 | 1235 | | |
1233 | 1236 | | |
1234 | 1237 | | |
| 1238 | + | |
| 1239 | + | |
1235 | 1240 | | |
1236 | 1241 | | |
1237 | 1242 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
1 | 3 | | |
2 | 4 | | |
3 | 5 | | |
| |||
6 | 8 | | |
7 | 9 | | |
8 | 10 | | |
9 | | - | |
| 11 | + | |
10 | 12 | | |
11 | | - | |
12 | 13 | | |
13 | 14 | | |
14 | 15 | | |
15 | 16 | | |
16 | 17 | | |
17 | 18 | | |
18 | 19 | | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
19 | 23 | | |
20 | 24 | | |
21 | 25 | | |
| |||
39 | 43 | | |
40 | 44 | | |
41 | 45 | | |
| 46 | + | |
| 47 | + | |
42 | 48 | | |
43 | 49 | | |
44 | 50 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
14 | 14 | | |
15 | 15 | | |
16 | 16 | | |
| 17 | + | |
17 | 18 | | |
18 | 19 | | |
19 | 20 | | |
| |||
0 commit comments