python / python/cpython

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

オープン
#138,453 コメント 1 件 リアクション 1 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

3.15 interpreter-core performance type-feature
主要言語
Python
スター
77.2k
フォーク
35.9k
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. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

まず _PyObject_InlineValues と _PyDictValues_AddToInsertionOrder を追跡し、次に issue で説明されている JIT パスを調べて、inline-value のサイズがどのように追跡されているかを理解します。挿入順序の格納が、オブジェクトの順序を変更せずに提案されたゼロデルタ動作を使用し、影響を受けるランタイムの動作が引き続き正しい状態になれば完了です。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
c, python
領域
backend, performance
issue の種類
リファクタリング
難易度
4/5
見積もり時間
3〜5日
活発さ
静か
明瞭さ
おおむね明確
初心者へのやさしさ
45/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。