python / python/cpython

Avoid creating temporary objects in BINARY_SLICE

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

還沒有人認領這個 Issue。

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

描述

Feature or enhancement

Proposal:

Currently BINARY_SLICE looks like this:

        op(_BINARY_SLICE, (container, start, stop -- res)) {
            PyObject *slice = _PyBuildSlice_ConsumeRefs(PyStackRef_AsPyObjectSteal(start),
                                                        PyStackRef_AsPyObjectSteal(stop));
            PyObject *res_o;
            if (slice == NULL) {
                res_o = NULL;
            }
            else {
                res_o = PyObject_GetItem(PyStackRef_AsPyObjectBorrow(container), slice);
                Py_DECREF(slice);
            }
            PyStackRef_CLOSE(container);
            ERROR_IF(res_o == NULL);
            res = PyStackRef_FromPyObjectSteal(res_o);
        }

Note that we build a slice just for getitem, only to throw it away immediately after.

We can scalar replace the slice in BINARY_SLICE after recording the types. If we record the container type, we can call the slice dispatcher underneath directly without boxing the slice object. See for example BINARY_OP_SUBSCR_LIST_SLICE

We'd need to guard on the type recorded.

I propose to do this in the current optimizer pass rather than the partial evaluation pass, as this is just a simple optimization within a single op, while the PE pass can handle inter-instruction optimization.

Has this already been discussed elsewhere?

No response given

Links to previous discussion of this feature:

No response

Linked PRs
  • gh-144590
  • gh-156318

貢獻指南

開啟貢獻指南

從這裡開始

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

研究方向

從 BINARY_SLICE 進入點開始,並將其與目前最佳化器 pass 中的 BINARY_OP_SUBSCR_LIST_SLICE 進行比較;同時閱讀部分求值 pass,以理解所述邊界。完成的標誌是記錄的容器類型受到保護,且切片分派避免建立暫時的切片物件,同時維持行為不變。

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

評估

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

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

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