Inspect module is prohibitively expensive to import
还没有人认领这个 Issue。
- 主要语言
- Python
- 星标
- 77.2k
- 派生
- 36k
- PR 合并指标
- PR 指标待抓取
描述
From post by @AlexWaygood in https://github.com/python/cpython/issues/117372#issuecomment-2047024740_:
I'm a little dismayed that dataclasses adds so much overhead. Does that imply that dataclasses shouldn't be used in the stdlib?
dataclassesis an unfortunately heavy import, yes. Although it imports relatively few modules itself, one of those modules isinspect, which has a huge import time (inspectimports half the standard library). In the past, I've looked into removing thedataclassesdependency oninspect, but couldn't see a way to do so without making the code significantly more ugly; the same goes when it comes to improving the import time forinspectitself. (In hindsight, I'd argueinspectshould probably have been a package rather than a huge single-file Python module... but hindsight is 20/20.)
Perhaps inspect could be refactored to limit the import-time cost.
Currently on my m3 mac pro, on the second attempt, the import times are 1.2ms and 7.9ms (cumulative):
cpython main @ ./python.exe -X importtime -c 'import inspect' 2> /dev/null ; ./python.exe -X importtime -c 'import inspect' 2>1 | grep -E '(cumul|inspect)'
import time: self [us] | cumulative | imported package
import time: 1184 | 7892 | inspect
I've searched the issue tracker and didn't see anything tracking this concern, so right now it's a naïve feature request.
Related: https://github.com/python/cpython/issues/108901
Linked PRs
- gh-119526
- gh-119546
- gh-144756
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
从 issue 中的 inspect 导入时命令开始,检查 inspect 模块,并使用 Python's -X importtime 输出确定其累计导入开销。完成的标准是重构 inspect 以降低导入时开销,同时保持其行为不变;已链接的 PR 表明这项工作已经在进行中。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- python
- 领域
- performance
- Issue 类型
- 功能
- 难度
- 5/5
- 预计耗时
- 一周以上
- 活跃度
- 停滞
- 描述清晰度
- 需要澄清
- 新手友好度
- 25/100