python / python/cpython

Use tagged ints for faster iteration

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

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

interpreter-core performance 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ả

Iteration over tuples and short lists is quite inefficient as we need to create an iterator object, only to have to destroy it again moments later. Not only that, fetching values from iterators involves additional indirection compared to fetching them from sequences.

Instead we can push a pair of values to the stack. For common sequences, like tuple, list, strings, some ranges and a few others, we push the sequence and the integer index (initially 0) to the stack. For other iterables, we push the iterator and NULL.

GET_ITER will have the signature:
iterable -- iter, index_or_null
FOR_ITER now has the signature:
iter, index_or_null -- iter, index_or_null, next.

What makes this efficient is tagged integers. By using tagged integers, no objects need to be created.

Examples
GET_ITER

[ <tuple at ...> ] -> [ <tuple at ...>, 0 ]

[ <file at ...> ] -> [ <file iterator at ...>, NULL ]

FOR_ITER

[ <tuple at ...>, 0 ] -> [ <tuple at ...>, 1, item0 ]

[ <file iterator at ...>, NULL ] -> [ <file iterator at ...>, NULL, line ]

Linked PRs
  • gh-132555
  • gh-132592
  • gh-135063
  • gh-135137

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 với các PR được liên kết gh-132555, gh-132592, gh-135063 và gh-135137 để xác định trạng thái triển khai hiện tại, sau đó truy vết các chữ ký stack của GET_ITER và FOR_ITER được mô tả ở đây. Được xem là hoàn tất khi các sequence phổ biến sử dụng các chỉ mục số nguyên được gắn thẻ mà không có iterator object, trong khi các iterable khác vẫn giữ một iterator và NULL.

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
compilers, performance
Loại issue
Tái cấu trúc
Độ khó
5/5
Thời gian dự kiến
Hơn một tuần
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
25/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.