Use tagged ints for faster iteration
還沒有人認領這個 Issue。
- 主要語言
- Python
- 星號
- 77.2k
- 分支
- 36k
- PR 合併指標
- PR 指標待擷取
描述
Iteration over tuples and short lists is quite inefficient as we need to create an iterator object, only to have to destroy it again moments later. Not only that, fetching values from iterators involves additional indirection compared to fetching them from sequences.
Instead we can push a pair of values to the stack. For common sequences, like tuple, list, strings, some ranges and a few others, we push the sequence and the integer index (initially 0) to the stack. For other iterables, we push the iterator and NULL.
GET_ITER will have the signature:
iterable -- iter, index_or_null
FOR_ITER now has the signature:
iter, index_or_null -- iter, index_or_null, next.
What makes this efficient is tagged integers. By using tagged integers, no objects need to be created.
Examples
GET_ITER
[ <tuple at ...> ] -> [ <tuple at ...>, 0 ]
[ <file at ...> ] -> [ <file iterator at ...>, NULL ]
FOR_ITER
[ <tuple at ...>, 0 ] -> [ <tuple at ...>, 1, item0 ]
[ <file iterator at ...>, NULL ] -> [ <file iterator at ...>, NULL, line ]
Linked PRs
- gh-132555
- gh-132592
- gh-135063
- gh-135137
貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
研究方向
從關聯的 PR gh-132555、gh-132592、gh-135063 和 gh-135137 開始,確定目前的實作狀態,然後追蹤此處描述的 GET_ITER 和 FOR_ITER 堆疊簽章。完成的標準是:常見序列使用帶標籤的整數索引而不使用迭代器物件,而其他可迭代物件保留一個迭代器和 NULL。
由索引模型根據 Issue 內容生成。
評估
- 技術堆疊
- python
- 領域
- compilers, performance
- Issue 類型
- 重構
- 難度
- 5/5
- 預估耗時
- 一週以上
- 活躍度
- 停滯
- 描述清晰度
- 基本清楚
- 新手友好度
- 25/100