python / python/cpython

Specialize long tail of binary operations using a table.

オープン
#100,239 コメント 3 件 リアクション 6 件 担当者 1 名 GitHub で見る

@brandtbucher がすでに取り組んでいます。

2022年12月14日 から。

interpreter-core performance type-feature
主要言語
Python
スター
77.2k
フォーク
36k
PR マージ指標
PR 指標を取得中

説明

There is a desire to specialize the remaining binary operations (including binary subscript).
However adding more and more specialized instructions is likely to make performance worse.

This idea is to have a lookup table of types pairs and function pointers. This is less efficient than inlining the code, but more extensible.

A single instruction can then support up to 256 specializations.
This will only work for immutable classes.

struct table_entry {
    PyTypeObject *left;
    PyTypeObject *left;
    binaryfunc *func;
};

TARGET(BINARY_OP_TABLE) {
    PyObject *lhs = SECOND();
    PyObject *rhs = TOP();
    Cache *cache = GET_CACHE();
    struct table_entry* entry = &THE_TABLE[cache->table_index];
    DEOPT_IF(Py_TYPE(lhs) != entry->left);
    DEOPT_IF(Py_TYPE(rhs) != entry->right);
    PyObject *res = entry->func(lhs, rhs);
    if (res == NULL) {
        goto error;
    }
    STACK_SHRINK(1);
    Py_DECREF(lhs);
    Py_DECREF(rhs);
    SET_TOP(res);
    DISPATCH();
}

An ancillary mapping of (left, right) -> index will be needed for efficient specialization.

It is probably worth keeping the most common operations int + int, float + float, etc. inline.

We can replace BINARY_SUBSCR with BINARY_OP ([]) to allow effective specialization of BINARY_SUBSCR
E.g. subscripting array.array[int] can be handled with the registration mechanism described below.

Registering binary functions at runtime

https://github.com/faster-cpython/ideas/discussions/162

Linked PRs
  • gh-128722
  • gh-128927
  • gh-128956
  • gh-128963
  • gh-129379
  • gh-129431
  • gh-129700
  • gh-132068
  • gh-132093
  • gh-132230
  • gh-132383
  • gh-132626
  • gh-144826
  • gh-148146
  • gh-148791
  • gh-149413
  • gh-149458
  • gh-156324
  • gh-156918

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

評価

この issue はまだ評価されていません。

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

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