python / python/cpython

Improve performance of inherited mapping assignment slot calls

オープン
#156,250 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

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. リポジトリをフォークし、ブランチを切って変更します。
  4. 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 を短くまとめたダイジェスト。