python / python/cpython

Adjust the value in objects' insertion order array to the index - position.

未關閉
#138,453 1 則留言 1 個 reaction 已指派 0 人 在 GitHub 檢視

還沒有人認領這個 Issue。

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

描述

Currently, we store an array of bytes at the end of the inline value array of objects to record the insertion order.
This is expensive as we need to compute not only the value to insert, but where to insert it. Here's the code:

     PyDictValues *values = _PyObject_InlineValues(owner_o);
     Py_ssize_t index = value_ptr - values->values;
     _PyDictValues_AddToInsertionOrder(values, index);

_PyObject_InlineValues requires some lookup, as it depends on tp->basicsize

    return (PyDictValues *)((char *)obj + tp->tp_basicsize);

but it is _PyDictValues_AddToInsertionOrder that is the slowest:

    int size = values->size;
    uint8_t *array = (uint8_t *)&values->values[values->capacity];
    array[size] = (uint8_t)ix;
    values->size = size+1;

If instead of storing the delta of the index and position, instead of just the index, for the majority of objects the insert order array will go from { 0, 1, 2, 3, 4, ... } to { 0, 0, 0, 0, 0, ... }
And if the delta is zero, we don't need to store anything.

     PyDictValues *values = _PyObject_InlineValues(owner_o);
     Py_ssize_t index = value_ptr - values->values;
     Py_ssize_t delta = index - values->size;
     if (delta != 0) {
        /* This is the expensive part */
        _PyDictValues_AddToInsertionOrder(values, delta);
    }
    values->size++;

In the JIT we can track the size of the inline values, and know when delta will be zero. Reducing the above code to

    values->size = KNOWN_SIZE;

貢獻指南

開啟貢獻指南

從這裡開始

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

研究方向

首先追蹤 _PyObject_InlineValues 和 _PyDictValues_AddToInsertionOrder,接著檢查 issue 中描述的 JIT 路徑,以了解如何追蹤 inline-value 的大小。當插入順序儲存使用提議的零增量行為而不改變物件順序,且受影響的執行階段行為仍然正確時,即可視為完成。

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

評估

技術堆疊
c, python
領域
backend, performance
Issue 類型
重構
難度
4/5
預估耗時
3-5 天
活躍度
冷清
描述清晰度
基本清楚
新手友好度
45/100

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

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