python / python/cpython

Improve performance of inherited mapping assignment slot calls

Đang mở
#156,250 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

type-feature
Ngôn ngữ chính
Python
Star
77.2k
Fork
35.9k
Chỉ số merge pull request
Chỉ số pull request đang chờ

Mô tả

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

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Hướng nghiên cứu

Bắt đầu bằng cách đọc các đường dẫn slot_mp_ass_subscript(), _PyObject_MakeTpCall() và wrapperdescr_call() được mô tả trong issue, sau đó kiểm tra PR được liên kết gh-156258. Công việc được xem là hoàn tất khi đường dẫn gán mapping được kế thừa tránh các tuple đối số tạm thời nhưng vẫn giữ fallback hiện có, và benchmark Counter được cung cấp cho thấy mức cải thiện đã báo cáo.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
python
Lĩnh vực
performance
Loại issue
Tính năng
Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức độ hoạt động
Đình trệ
Độ rõ ràng
Khá rõ ràng
Mức phù hợp với người mới
35/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.