Remove Incref/Decref of Specific Immortal Objects
還沒有人認領這個 Issue。
- 主要語言
- Python
- 星號
- 77.2k
- 分支
- 36k
- PR 合併指標
- PR 指標待擷取
描述
Feature or enhancement
Proposal:
In various places we incref or decref an object that we already know is a specific immortal object, like None. For example, see gh-117393. These operations are unnecessary and the (small) cost can be avoided.
We could simply drop the increfs and decrefs, but there is value in communicating that we would have done the op if the object weren't known to be immortal. (Furthermore, there's the very, very remote chance that some known-immortal object might be made mortal again some day, for some currently unknown reason. It would be quite hard at the point to know that we should add back in the incref or decref we had removed.)
Here are the options I see:
- leave the existing code alone
- replace the existing code with comments
- replace the existing code with a noop macro (in the internal API)
We probably wouldn't make all the changes we could find all at once, but could at the least point to this issue for the decision on what should be done. If we do add a macro then we'd do it relative to this issue.
Personally, I'd go with the macro. It would communicate the intent to readers just as well as the existing code does or as a comment would. I imagine it would look something like the following:
#ifdef Py_DEBUG
# define Py_INCREF_IMMORTAL(OBJ) \
assert(_Py_IsImmortal(OBJ)
# define Py_DECREF_IMMORTAL(OBJ) \
assert(_Py_IsImmortal(OBJ)
#else
# define Py_INCREF_IMMORTAL(OBJ)
# define Py_DECREF_IMMORTAL(OBJ)
#endif
For None we could be even more explicit:
#define Py_INCREF_NONE() \
Py_INCREF_IMMORTAL(Py_None)
#define Py_DECREF_NONE() \
Py_DECREF_IMMORTAL(Py_None)
// or
#define Py_INCREF_NONE(OBJ) \
do { \
assert(OBJ == Py_None) \
Py_INCREF_IMMORTAL(OBJ) \
} while (0)
#define Py_DECREF_NONE(OBJ) \
do { \
assert(OBJ == Py_None) \
Py_DECREF_IMMORTAL(OBJ) \
} while (0)
CC @eduardo-elizondo
Has this already been discussed elsewhere?
No response given
Links to previous discussion of this feature:
No response
貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
研究方向
先從提案和其中引用的 gh-117393 討論開始,然後找出針對已知 immortal objects 的現有 incref/decref 操作。比較所述的三個選項,包括提議的 Py_DEBUG 行為,並確定採用哪種 API 選擇和範圍才算完成;issue 中沒有指定檔案或測試。
由索引模型根據 Issue 內容生成。
評估
- 技術堆疊
- c
- 領域
- backend
- Issue 類型
- 功能
- 難度
- 5/5
- 預估耗時
- 一週以上
- 活躍度
- 停滯
- 描述清晰度
- 需要釐清
- 新手友好度
- 25/100