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
{{ message }}
Repository navigation
Defer SciPy and SymPy imports to cut import spatialmath time (~800 ms → ~150 ms) #234
Follow-up to #198, which deferred matplotlib.pyplot until something actually plots. Its description noted that scipy.interpolate and sympy cost about as much. They are now the main remaining costs of import spatialmath.
Where the time goes
Measured with python -X importtime -c "import spatialmath" on Python 3.12, macOS, Apple M1 (8 GB), warm cache. Cold-start times will be higher.
Module
Cost
Imported by
scipy.interpolate + scipy.spatial.transform
~455 ms
base/quaternions.py:19, spline.py:14-15 (both at top level)
sympy (when installed)
~200 ms
base/symbolic.py:19, via base/argcheck.py
matplotlib (no pyplot)
~100 ms
geom2d.py:13from matplotlib.path import Path. #198 kept this on purpose because Polygon2 needs it for its geometry
numpy
~40 ms
Total
~800 ms
SciPy: nothing needs it at import time
base/quaternions.py: the only user is qrand(). When the angle is limited, it does inverse-CDF sampling through _generate_inv_cdf_sin_squared_interp(), which is already @lru_cache'd. Every user pays ~440 ms for this one option. qslerp and the rest of the module don't use SciPy. Fix: move the import into that function, and make the -> interpolate.interp1d annotation a string or put it under TYPE_CHECKING.
spline.py: CubicSpline, RotationSpline, Rotation and BSpline are only used inside methods. Fix: import them inside those methods.
Both must change. If only one does, the other still loads scipy.interpolate. SciPy stays a hard dependency; only when it loads changes.
Fit b spline se3 #129 (open) adds more top-level SciPy imports to spline.py (scipy.optimize, splrep) and would need the same treatment.
SymPy: avoid importing it just to check types
SymPy is optional. base/symbolic.py imports it inside a try so that issymbol() can check for sympy.Expr. But if "sympy" not in sys.modules, no argument can possibly be a SymPy expression. So issymbol() can return False without importing SymPy, and only import it once the user has imported SymPy themselves. This is a separate change from the SciPy one because argcheck is on almost every code path, so it needs its own testing (including the :SymPy: supported paths).
Expected result
About 150 ms for import spatialmath, and none of SciPy or SymPy loaded until a feature actually uses them. A regression test along the lines of #198's could assert that neither scipy nor sympy is in sys.modules after import spatialmath.
Follow-up to #198, which deferred
matplotlib.pyplotuntil something actually plots. Its description noted thatscipy.interpolateandsympycost about as much. They are now the main remaining costs ofimport spatialmath.Where the time goes
Measured with
python -X importtime -c "import spatialmath"on Python 3.12, macOS, Apple M1 (8 GB), warm cache. Cold-start times will be higher.scipy.interpolate+scipy.spatial.transformbase/quaternions.py:19,spline.py:14-15(both at top level)sympy(when installed)base/symbolic.py:19, viabase/argcheck.pymatplotlib(no pyplot)geom2d.py:13from matplotlib.path import Path. #198 kept this on purpose becausePolygon2needs it for its geometrynumpySciPy: nothing needs it at import time
base/quaternions.py: the only user isqrand(). When the angle is limited, it does inverse-CDF sampling through_generate_inv_cdf_sin_squared_interp(), which is already@lru_cache'd. Every user pays ~440 ms for this one option.qslerpand the rest of the module don't use SciPy. Fix: move the import into that function, and make the-> interpolate.interp1dannotation a string or put it underTYPE_CHECKING.spline.py:CubicSpline,RotationSpline,RotationandBSplineare only used inside methods. Fix: import them inside those methods.scipy.interpolate. SciPy stays a hard dependency; only when it loads changes.spline.py(scipy.optimize,splrep) and would need the same treatment.SymPy: avoid importing it just to check types
SymPy is optional.
base/symbolic.pyimports it inside atryso thatissymbol()can check forsympy.Expr. But if"sympy" not in sys.modules, no argument can possibly be a SymPy expression. Soissymbol()can returnFalsewithout importing SymPy, and only import it once the user has imported SymPy themselves. This is a separate change from the SciPy one becauseargcheckis on almost every code path, so it needs its own testing (including the:SymPy: supportedpaths).Expected result
About 150 ms for
import spatialmath, and none of SciPy or SymPy loaded until a feature actually uses them. A regression test along the lines of #198's could assert that neitherscipynorsympyis insys.modulesafterimport spatialmath.