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 · 2 comments
Open

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

markshannon opened this issue Dec 14, 2022 · 2 comments
Assignees
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) 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

Linked PRs

@markshannon markshannon added the performance Performance or resource usage label Dec 14, 2022
@iritkatriel iritkatriel added the interpreter-core (Objects, Python, Grammar, and Parser dirs) label Nov 28, 2023
iritkatriel added a commit to iritkatriel/cpython that referenced this issue Jan 10, 2025
iritkatriel added a commit to iritkatriel/cpython that referenced this issue Jan 10, 2025
iritkatriel added a commit to iritkatriel/cpython that referenced this issue Jan 14, 2025
iritkatriel added a commit to iritkatriel/cpython that referenced this issue Jan 14, 2025
iritkatriel added a commit to iritkatriel/cpython that referenced this issue Jan 15, 2025
iritkatriel added a commit to iritkatriel/cpython that referenced this issue Jan 16, 2025
iritkatriel added a commit to iritkatriel/cpython that referenced this issue Jan 16, 2025
iritkatriel added a commit to iritkatriel/cpython that referenced this issue Jan 16, 2025
@encukou
Copy link
Member

encukou commented Jan 17, 2025

#128722 broke repeated runs of test.test_ctypes.test_random_things.CallbackTracbackTestCase.test_FloatDivisionError, on several refleak buildbots e.g. https://buildbot.python.org/#/builders/920/builds/1287

Is it possible to fix this today?

@Eclips4
Copy link
Member

Eclips4 commented Jan 17, 2025

#128722 broke repeated runs of test.test_ctypes.test_random_things.CallbackTracbackTestCase.test_FloatDivisionError, on several refleak buildbots e.g. https://buildbot.python.org/#/builders/920/builds/1287

Is it possible to fix this today?

I'll take care of it!

Eclips4 added a commit that referenced this issue Jan 19, 2025
srinivasreddy pushed a commit to srinivasreddy/cpython that referenced this issue Jan 21, 2025
iritkatriel added a commit to iritkatriel/cpython that referenced this issue Jan 27, 2025
iritkatriel added a commit to iritkatriel/cpython that referenced this issue Jan 27, 2025
eendebakpt pushed a commit to eendebakpt/cpython that referenced this issue Jan 29, 2025
iritkatriel added a commit to iritkatriel/cpython that referenced this issue Feb 3, 2025
iritkatriel added a commit to iritkatriel/cpython that referenced this issue Feb 4, 2025
iritkatriel added a commit to iritkatriel/cpython that referenced this issue Feb 5, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) performance Performance or resource usage
Projects
None yet
Development

No branches or pull requests

5 participants