python / python/cpython

Calling comprehensions always makes CALL_PY_EXACT_ARGS miss

未關閉
#93,827 3 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

還沒有人認領這個 Issue。

interpreter-core type-feature
主要語言
Python
星號
77.2k
分支
36k
平均合併
1 天 9 小時
30 天內合併 PR
558

描述

Consider the function

def f():
    return [x for x in range(2)]
for i in range(20):
    f() # warmup
from dis import dis
dis(f, adaptive=True)
dis(f)
  1           0 RESUME_QUICK             0

  2           2 LOAD_CONST               1 (<code object <listcomp> at 0x000001DA71743920, file "<stdin>", line 2>)
              4 MAKE_FUNCTION            0
              6 LOAD_GLOBAL_BUILTIN      1 (NULL + range)
             18 LOAD_CONST               2 (2)
             20 CALL_BUILTIN_CLASS       1
             30 GET_ITER
             32 CALL_PY_EXACT_ARGS       0
             42 POP_TOP
             44 LOAD_CONST               0 (None)
             46 RETURN_VALUE

Disassembly of <code object <listcomp> at 0x000001DA71743920, file "<stdin>", line 2>:
  2           0 RESUME_QUICK             0
              2 BUILD_LIST               0
              4 LOAD_FAST                0 (.0)
        >>    6 FOR_ITER_RANGE           4 (to 18)
             10 STORE_FAST__LOAD_FAST     1 (x)
             12 LOAD_FAST                1 (x)
             14 LIST_APPEND              2
             16 JUMP_BACKWARD_QUICK      6 (to 6)
        >>   18 RETURN_VALUE

That CALL_PY_EXACT_ARGS always misses at

DEOPT_IF(func->func_version != read_u32(cache->func_version), CALL);

because the function is created by MAKE_FUNCION so it will never have the same func_version as the previous time.

CALL_PY_EXACT_ARGS misses roughly 5% of the time in pyperformance, and this might decrease that a hair.

Maybe this is insignificant enough that it won't matter, but it would theoretically be nice to statically decide not to specialize these calls.

cc @markshannon

貢獻指南

開啟貢獻指南

從這裡開始

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

研究方向

先追蹤由 MAKE_FUNCTION 建立的函式所使用的 CALL_PY_EXACT_ARGS 特化,重點關注 issue 中描述的 DEOPT_IF func_version 檢查。使用反組譯範例作為 reproducer,並比較 pyperformance 結果;當這些 comprehension 呼叫能在靜態階段被避免,或不再不必要地 miss,且報告中的 benchmark 沒有回歸時,即視為完成。

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

評估

技術堆疊
python
領域
performance
Issue 類型
缺陷
難度
4/5
預估耗時
3-5 天
活躍度
停滯
描述清晰度
基本清楚
新手友好度
38/100

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

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