Incorrect MRO cache entry is populated for types that do not have a version assigned
还没有人认领这个 Issue。
- 主要语言
- Python
- 星标
- 77.2k
- 派生
- 35.9k
- PR 合并指标
- PR 指标待抓取
描述
Bug report
Bug description:
_PyType_LookupRefAndVersion will populate the incorrect cache entry if it is called with a type that does not have a version tag (tp_version_tag is 0). I think this only impacts performance, not correctness. The next call to _PyType_LookupRefAndVersion for the same (type, name) pair will miss the cache, but will populate the correct entry, so any subsequent calls will hit the cache. Additionally, this may create some amount of unnecessary thrash on the cache.
The root of the issue is that the version tag is an input to the hash that is used to determine the cache entry and we do not recompute the hash, and the associated cache entry, after assigning a version.
_PyType_LookupRefAndVersion first computes the cache entry:
If the cache entry doesn't match, and a type version was assigned successfully, it populates the same entry:
The next call for the same (type, name) pair, will find a different entry, because the type version is used to compute the hash that indexes into the cache:
It looks like the change to use the same entry was introduced in gh-113930. cc @DinoV
CPython versions tested on:
3.13, 3.14
Operating systems tested on:
Linux
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
从 Objects/typeobject.c 中的 _PyType_LookupRefAndVersion 以及所引用行附近的缓存哈希计算开始。跟踪无版本路径和新分配版本路径,然后验证重复查找使用的是按已分配版本索引的缓存条目,而不是留下不必要的条目。合适的回归检查应展示修正后的缓存行为,同时不改变查找结果。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- c, python
- 领域
- performance
- Issue 类型
- 缺陷
- 难度
- 4/5
- 预计耗时
- 3-5 天
- 活跃度
- 停滞
- 描述清晰度
- 描述清楚
- 新手友好度
- 45/100