Defer SciPy and SymPy imports to cut `import spatialmath` time (~800 ms → ~150 ms)
維護者通常 1 天內回覆
還沒有人認領這個 Issue。
評估
- 難度
- 4/5
- 預估耗時
- 3-5 天
- 新手友好度
- 68/100
- Issue 類型
- 重構
- 描述清晰度
- 描述清楚
- 活躍度
- 活躍
- 技術堆疊
- python
- 領域
- performance, testing
研究方向
Start by reading the top-level imports and mentioned methods in base/quaternions.py, spline.py, base/symbolic.py, and base/argcheck.py. Compare the import-time regression approach from #198, then verify that import spatialmath leaves neither scipy nor sympy in sys.modules while the affected feature and SymPy-supported paths still work.
由索引模型根據 Issue 內容生成。
描述
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:13 from 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 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.- Both must change. If only one does, the other still loads
scipy.interpolate. SciPy stays a hard dependency; only when it loads changes. - #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.
- 主要語言
- Python
- 星號
- 642
- 分支
- 102
- 平均合併
- 11 分鐘
- 30 天內合併 PR
- 1
環境準備
這個專案沒有提供開發容器、Dockerfile 或貢獻指南,環境需要你自己搭建:先看它的 README,通用步驟見我們的新手貢獻指南。
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
rai-opensource/spatialmath-python 的其他 Issue
-
documentation
難度 2/5 1-3 小時 新手友好度 74/100
rai-opensource/spatialmath-python#235 ·
維護者通常 1 天內回覆
-
enhancement
難度 2/5 1-3 小時 新手友好度 65/100
rai-opensource/spatialmath-python#32 · 4 則留言 ·
維護者通常 1 天內回覆
-
Line3.isparallel()/__or__ still scale-dependent, like the distance() bug fixed in #224可能已有人在做 關聯的 PR 仍在進行中或已合併。 未關閉
難度 3/5 1-2 天 新手友好度 72/100
rai-opensource/spatialmath-python#231 ·
維護者通常 1 天內回覆
-
難度 2/5 1-3 小時 新手友好度 50/100
rai-opensource/spatialmath-python#177 ·
維護者通常 1 天內回覆
-
難度 3/5 1-2 天 新手友好度 42/100
rai-opensource/spatialmath-python#171 ·
維護者通常 1 天內回覆
查看 rai-opensource/spatialmath-python 的全部 Issue
相似的 Issue
-
needs triage
難度 2/5 1-3 小時 新手友好度 76/100
維護者通常 1 天內回覆
-
json_params_matcher fails on falsy top-level JSON primitives (0, False, "")可能已有人在做 @mayureshsonawane17 今天認領。 未關閉Waiting for: Product Owner
難度 2/5 1-3 小時 新手友好度 84/100
維護者通常 5 天內回覆
-
難度 2/5 1-3 小時 新手友好度 75/100
維護者通常 1 天內回覆
-
難度 1/5 1 小時以內 新手友好度 88/100
ayghri/i-have-adhd#249 ·
維護者通常 2 天內回覆
-
難度 2/5 1-3 小時 新手友好度 72/100
modelscope/FunASR#3762 ·
維護者通常 1 天內回覆