Hacktoberfest 2026:维护者为十月标记出来的 issue,仍然开放、适合新手。 浏览 Hacktoberfest issue

Very long delay (10–20 s) on first plot in a fresh environment: Matplotlib font-cache build

未关闭 适合新手
#235 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

难度
2/5
预计耗时
1-3 小时
新手友好度
74/100
Issue 类型
文档
描述清晰度
基本清楚
活跃度
活跃
技术栈
python
领域
documentation

调研方向

首先定位项目的 troubleshooting 或安装文档,并了解其中如何组织特定于环境的说明。添加一条简短的“Slow first plot”说明,涵盖 Matplotlib 冷启动时字体缓存导致的原因,以及 issue 中描述的 Docker、CI 和 sandbox 缓存持久化修复方案;当用户能够理解延迟原因并选择缓存解决方案时,即表示完成。

由索引模型根据 Issue 内容生成。

描述

documentation
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 MPLCONFIGDIR directory between jobs (e.g. actions/cache on ~/.cache/matplotlib), or run the line above in a setup step.
  • Sandboxes: set MPLCONFIGDIR to 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,通用步骤见我们的新手贡献指南。

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

rai-opensource/spatialmath-python 的其他 Issue

查看 rai-opensource/spatialmath-python 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。