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
PR 合并指标
PR 指标待抓取

描述

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 摘要。