python / python/cpython

Avoid `exc=None; del exc` in bytecode where unnecessary

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

還沒有人認領這個 Issue。

3.13 interpreter-core performance type-feature
主要語言
Python
星號
77.2k
分支
36k
PR 合併指標
PR 指標待擷取

描述

See https://github.com/faster-cpython/ideas/issues/490.

To break reference cycles, the compiler has an implicit del e at the end of an exception handler. To make UnboundLocalErrors impossible during that deletion, the compiler adds e=None; del e instead.

However, the e=None part is unnecessary in many situations, namely, when we can prove that e is guaranteed to be bound (almost always!).

This can reduce the size of some bytecode objects. For example, the four <---- lines below can be deleted:

>>> def f():
...     try: pass
...     except Exception as e: pass
...
>>> dis(f)
  1           0 RESUME                   0

  2           2 LOAD_CONST               0 (None)
              4 RETURN_VALUE
              6 PUSH_EXC_INFO

  3           8 LOAD_GLOBAL              0 (Exception)
             20 CHECK_EXC_MATCH
             22 POP_JUMP_IF_FALSE       11 (to 46)
             24 STORE_FAST               0 (e)
             26 POP_EXCEPT
             28 LOAD_CONST               0 (None)  <---------
             30 STORE_FAST               0 (e)     <---------
             32 DELETE_FAST              0 (e)     
             34 LOAD_CONST               0 (None)
             36 RETURN_VALUE
             38 LOAD_CONST               0 (None)  <---------
             40 STORE_FAST               0 (e)     <---------
             42 DELETE_FAST              0 (e)
             44 RERAISE                  1
        >>   46 RERAISE                  0
        >>   48 COPY                     3
             50 POP_EXCEPT
             52 RERAISE                  1
ExceptionTable:
  6 to 24 -> 48 [1] lasti
  38 to 46 -> 48 [1] lasti
  • PR: gh-99361

貢獻指南

開啟貢獻指南

從這裡開始

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

研究方向

從連結的 faster-cpython/ideas issue 490 和 PR gh-99361 開始,然後檢查產生例外處理常式清理位元組碼的 Python 編譯器路徑。以 dis(f) 範例為基準;在能保證例外變數已繫結時省略不必要的 exc=None; del exc 序列,且不改變例外語義,即視為完成。

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

評估

技術堆疊
python
領域
compilers
Issue 類型
重構
難度
4/5
預估耗時
3-5 天
活躍度
停滯
描述清晰度
描述清楚
新手友好度
25/100

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

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