python / python/cpython

Optimize reference tracking and eliminate branching during returns and yields

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

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

3.15 interpreter-core topic-JIT
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ả

There are a few optimizations we can make to returns and yields, but they are somewhat related so I'm grouping them into a single issue.

During a return, the VM needs to make the returned reference "heap safe" (convert any borrowed references to strong references) and then pop and clear the frame, which can involve quite a lot of decrefs.
During a yield, the VM needs to make the returned reference heap safe and then pop the frame, but not clear it.

We want to minimize the amount of refcounting operations that we do.

Before we can do much else, we should split RETURN_VALUE and YIELD_VALUE into micro-ops:

macro(RETURN_VALUE) = _MAKE_HEAP_SAFE + _RETURN_VALUE

macro(YIELD_VALUE) = _MAKE_HEAP_SAFE + _YIELD_VALUE

so that we can optimize the uops independently

Eliminate making heap safe if we know that reference already is

If TOS is a strong reference _MAKE_HEAP_SAFE -> _NOP

Avoid the branch in _RETURN_VALUE by splitting into _RETURN_VALUE_GEN and _RETURN_VALUE_FUNC

To avoid using up opcodes, we'll need to do this in the JIT, not the interpreter.

Track borrows/immortals to optimize clearing frames

E.g. if a frame has four local variables, a, b, c, d but we can tell that a and d are immortal, or borrowed, then instead of looping over all the variables, we could emit code just to decref b and c.
This might end up bloating the code, as we would need to inline the decrefs, but it could be quite a lot faster.

Linked PRs
  • gh-144414
  • gh-146320

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 macro RETURN_VALUE và YIELD_VALUE, sau đó lần theo _MAKE_HEAP_SAFE, _RETURN_VALUE và _YIELD_VALUE qua trình thông dịch và JIT. So sánh các công việc liên quan gh-144414 và gh-146320 trước khi quyết định phạm vi; để hoàn thành sẽ cần các đường dẫn return và yield được tối ưu độc lập và giảm việc đếm tham chiếu không cần thiết mà không làm thoái lui hành vi.

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
Cần làm rõ
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.