python / python/cpython

Improve performance by using reference stealing methods

未关闭
#150,942 19 条评论 8 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

easy extension-modules performance type-feature
主要语言
Python
星标
77.2k
派生
35.9k
PR 合并指标
PR 指标待抓取

描述

Feature or enhancement

Proposal:

We can improve performance of cpython by using stealing methods (_PyList_AppendTakeRef, _PyDict_SetItem_Take2, PyTuple_FromPairSteal etc.) instead of the normal ones. Using stealing methods we typically avoid an incref/decref pair and sometimes error handling is simplified. We should only make changes where it makes a performance impact, the code is not more complex and the modules or methods affected are performance critical.

A list of possible cases (there might be more).

Location Module / context Helper
Modules/_csv.c:688 csv reader, per field _PyList_AppendTakeRef
Modules/pyexpat.c:472 pyexpat StartElement, per attribute _PyDict_SetItem_Take2
Modules/_elementtree.c:3351 ElementTree C accel attrib dict _PyDict_SetItem_Take2
Modules/_io/bytesio.c:577 io.BytesIO.readlines _PyList_AppendTakeRef
Modules/_io/iobase.c:756 io.IOBase.readlines (generic) _PyList_AppendTakeRef
Objects/frameobject.c:639 & 658 frame f_locals / vars() / tracing _PyList_AppendTakeRef
Modules/_pickle.c:5138 pickle Pickler memo copy _PyDict_SetItem_Take2
Modules/_io/textio.c:2337 & 2343 io.TextIOWrapper readline (multi-chunk) _PyList_AppendTakeRef
Modules/_asynciomodule.c:1213 asyncio future done-callback compaction _PyList_AppendTakeRef

Update: Most cases already have an open PR, for new ones please open a separate issue.

Linked PRs
  • gh-150943
  • gh-150945
  • gh-150995
  • gh-151002
  • gh-151062
  • gh-151209
  • gh-151608
  • gh-153013
  • gh-154860
  • gh-155171
  • gh-155304
  • gh-155922
  • gh-155940
  • gh-155957
  • gh-156055
  • gh-156199

贡献指南

打开贡献指南

从这里开始

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

调研方向

检查列出的 C 位置,包括 Modules/_csv.c、Modules/pyexpat.c、Modules/_elementtree.c,以及 _io、Objects、_pickle 和 _asynciomodule 中的文件。首先检查已关联的 PR,确认哪些情况已经覆盖;任何剩余工作都应拆分为一个单独且范围明确的 issue,并配有相关的性能测试。完成的标准是:在不增加复杂性的情况下修改一个选定的性能关键情况,并验证其行为和性能。

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

评估

技术栈
c, python
领域
backend
Issue 类型
功能
难度
4/5
预计耗时
3-5 天
活跃度
停滞
描述清晰度
需要澄清
新手友好度
25/100

把新 issue 发到你的邮箱

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