python / python/cpython

Optimize reference tracking and eliminate branching during returns and yields

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

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

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. リポジトリをフォークし、ブランチを切って変更します。
  4. 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
見積もり時間
1週間以上
活発さ
停滞
明瞭さ
説明が足りない
初心者へのやさしさ
25/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。