Very long delay (10–20 s) on first plot in a fresh environment: Matplotlib font-cache build
維護者通常 1 天內回覆
還沒有人認領這個 Issue。
評估
- 難度
- 2/5
- 預估耗時
- 1-3 小時
- 新手友好度
- 74/100
- Issue 類型
- 文件
- 描述清晰度
- 基本清楚
- 活躍度
- 活躍
- 技術堆疊
- python
研究方向
先找到專案的 troubleshooting 或安裝文件,並檢視其中如何組織環境特定的說明。新增一則簡短的「Slow first plot」說明,涵蓋 Matplotlib 冷啟動時字型快取造成的原因,以及 issue 中描述的 Docker、CI 和 sandbox 快取持久化修正方案;當使用者能夠理解延遲原因並選擇快取解決方案時,即表示完成。
由索引模型根據 Issue 內容生成。
描述
Symptom
In a fresh environment, the first plot (or, before #198, plain import spatialmath) can take 10–20 s. Later runs on the same machine are fast, so the delay is hard to reproduce on a long-lived development machine, but it can recur on every run in containers or CI.
Cause
This is Matplotlib behaviour, not spatialmath code. The first time Matplotlib's font manager runs with no font cache, it scans every font installed on the system to build one. That's the "Matplotlib is building the font cache; this may take a moment" message. The cache is stored in MPLCONFIGDIR, which by default is ~/.cache/matplotlib on Linux and ~/.matplotlib on macOS. If that directory doesn't persist between runs, every run pays the full cost. This commonly happens with:
- Docker containers and CI runners started fresh each time
- sandboxed build/test tools that don't keep the home directory's cache
- new virtual environments or users with a different
MPLCONFIGDIR
Measurements
Apple M1 (8 GB), macOS, Python 3.12. "Empty cache" means MPLCONFIGDIR pointed at an empty directory.
| Empty font cache | Cache already built | |
|---|---|---|
import matplotlib.pyplot |
17.0 s | — |
import spatialmath, before #198 (f6a572c6^) |
17.6 s | 1.1 s |
import spatialmath, after #198 (current master) |
0.75 s | 0.7 s |
Before #198, import spatialmath imported matplotlib.pyplot, so every spatialmath user paid this on a cold cache, including code that never plots. Since #198 the import is lazy, so the cost appears only at the first plot, and code that never plots never pays it.
How long the scan takes depends on how many fonts are installed. A minimal Linux container will usually be faster than macOS, but several seconds is typical.
Fix
spatialmath can't avoid this without dropping Matplotlib. If you plot in a fresh environment, keep the cache between runs:
- Docker: build the cache when building the image, so it's baked in:
RUN python -c "import matplotlib.pyplot" - CI: cache the
MPLCONFIGDIRdirectory between jobs (e.g.actions/cacheon~/.cache/matplotlib), or run the line above in a setup step. - Sandboxes: set
MPLCONFIGDIRto a persistent, writable directory.
Proposed docs change
Add a short "Slow first plot" troubleshooting note to the docs, with the explanation and fixes above.
Related: #198 (lazy Matplotlib import), #234 (remaining import-time costs from SciPy/SymPy).
- 主要語言
- Python
- 星號
- 643
- 分支
- 102
- 平均合併
- 11 分鐘
- 30 天內合併 PR
- 1
環境準備
這個專案沒有提供開發容器、Dockerfile 或貢獻指南,環境需要你自己搭建:先看它的 README,通用步驟見我們的新手貢獻指南。
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
rai-opensource/spatialmath-python 的其他 Issue
-
enhancement
難度 2/5 1-3 小時 新手友好度 65/100
rai-opensource/spatialmath-python#32 · 4 則留言 ·
維護者通常 1 天內回覆
-
難度 3/5 1-2 天 新手友好度 66/100
rai-opensource/spatialmath-python#236 ·
維護者通常 1 天內回覆
-
enhancement
難度 4/5 3-5 天 新手友好度 68/100
rai-opensource/spatialmath-python#234 ·
維護者通常 1 天內回覆
-
Line3.isparallel()/__or__ still scale-dependent, like the distance() bug fixed in #224可能已有人在做 @Doribelove 於 10 天前認領。 未關閉
難度 3/5 1-2 天 新手友好度 72/100
rai-opensource/spatialmath-python#231 ·
維護者通常 1 天內回覆
-
難度 2/5 1-3 小時 新手友好度 50/100
rai-opensource/spatialmath-python#177 ·
維護者通常 1 天內回覆
查看 rai-opensource/spatialmath-python 的全部 Issue
相似的 Issue
-
changelog investigate
難度 2/5 1-3 小時 新手友好度 62/100
ramnes/notion-sdk-py#409 ·
-
good first issue help wanted
難度 2/5 1-3 小時 新手友好度 72/100
lindicaphxag-tech/kaggle#28 ·
維護者通常 1 天內回覆
-
難度 2/5 1-3 小時 新手友好度 62/100
BSData/horus-heresy-3rd-edition#3211 ·
維護者通常 1 天內回覆
-
bug needs-triage
難度 2/5 1-3 小時 新手友好度 70/100
維護者通常 1 天內回覆
-
bug tests
難度 2/5 1-3 小時 新手友好度 76/100
維護者通常 1 天內回覆