python / python/cpython

Optimize reference tracking and eliminate branching during returns and yields

未關閉
#144,540 5 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

還沒有人認領這個 Issue。

3.15 interpreter-core topic-JIT
主要語言
Python
星號
77.2k
分支
35.9k
PR 合併指標
PR 指標待擷取

描述

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

貢獻指南

開啟貢獻指南

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

研究方向

先閱讀 RETURN_VALUE 和 YIELD_VALUE 巨集,然後在直譯器和 JIT 中追蹤 _MAKE_HEAP_SAFE、_RETURN_VALUE 和 _YIELD_VALUE。決定範圍前,先比較相關工作 gh-144414 和 gh-146320;完成這項工作需要分別最佳化 return 和 yield 路徑,並減少不必要的參照計數,同時不導致行為回歸。

由索引模型根據 Issue 內容生成。

評估

技術堆疊
python
領域
compilers, performance
Issue 類型
重構
難度
5/5
預估耗時
一週以上
活躍度
停滯
描述清晰度
需要釐清
新手友好度
25/100

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。