The Wayback Machine - https://web.archive.org/web/20221223105120/https://github.com/python/cpython/issues/100239
Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Specialize long tail of binary operations using a table. #100239

Open
markshannon opened this issue Dec 14, 2022 · 0 comments
Open

Specialize long tail of binary operations using a table. #100239

markshannon opened this issue Dec 14, 2022 · 0 comments
Assignees
Labels
performance Performance or resource usage

Comments

@markshannon
Copy link
Member

markshannon commented Dec 14, 2022

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

faster-cpython/ideas#162

@markshannon markshannon added the performance Performance or resource usage label Dec 14, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
performance Performance or resource usage
Projects
None yet
Development

No branches or pull requests

2 participants