python / python/cpython

Specialize long tail of binary operations using a table.

Open
#100,239 3 comments 6 reactions 1 assignee View on GitHub

@brandtbucher is already working on this.

Since Dec 14, 2022.

interpreter-core performance type-feature
Dominant language
Python
Stars
77.2k
Forks
36k
PR merge metrics
PR metrics pending

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.