python / python/cpython

Improve performance of inherited mapping assignment slot calls

未关闭
#156,250 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

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

描述

Feature or enhancement

Proposal:

__setitem__ and __delitem__ share the mp_ass_subscript slot. If a Python subclass of a C mapping overrides only one of them, slot_mp_ass_subscript() is used for both operations.

For example, collections.Counter overrides __delitem__ but inherits dict.__setitem__. Assigning an item looks up the inherited wrapper_descriptor and calls it through the generic tp_call path. Since PyWrapperDescr_Type does not support vectorcall, _PyObject_MakeTpCall() and wrapperdescr_call() each create a temporary argument tuple before reaching dict's C slot.

Reproducer:

from collections import Counter
from time import perf_counter_ns

obj = Counter()

start = perf_counter_ns()
for index in range(20_000_000):
    obj[0] = index

print((perf_counter_ns() - start) / 1_000_000)

On macOS 15.7.1 arm64 with a release build of main (d856402e7176e5a03c5c578f0efc1be968be3257), using two warmups and nine alternating-order samples:

main:      1402.07 ms median (1376.93–1445.12 ms)
prototype:  859.97 ms median (846.24–902.74 ms)

The prototype calls the inherited C slot directly when lookup returns the expected unbound mapping slot wrapper, and otherwise keeps the existing call path. This reduced the benchmark time by 38.66%.

The relevant call-path files are unchanged at b062727097e997bcb900e11503d3248daac903da.

Has this already been discussed elsewhere?

This is a minor feature, which does not need previous discussion elsewhere

Links to previous discussion of this feature:

No response

Linked PRs
  • gh-156258

贡献指南

打开贡献指南

从这里开始

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

调研方向

先阅读 issue 中描述的 slot_mp_ass_subscript()、_PyObject_MakeTpCall() 和 wrapperdescr_call() 路径,然后检查关联的 PR gh-156258。完成的标准是,继承的 mapping 赋值路径在保留现有 fallback 的同时避免临时参数元组,并且提供的 Counter 基准测试显示出报告中的改进。

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

评估

技术栈
python
领域
performance
Issue 类型
功能
难度
4/5
预计耗时
3-5 天
活跃度
停滞
描述清晰度
基本清楚
新手友好度
35/100

把新 issue 发到你的邮箱

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