Skip to content

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

Description

@markshannon

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

Activity

  1. added 9 commits that reference this issue on Jan 10, 2025
  2. encukou commented on Jan 17, 2025

    @encukou
    Member

    #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?

  3. Eclips4 commented on Jan 17, 2025

    @Eclips4
    Member

    #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!

  4. added a commit that references this issue on Jan 19, 2025
  5. 17 remaining items

  6. added a commit that references this issue on May 1, 2025
  7. added a commit that references this issue on Feb 19, 2026
  8. added a commit that references this issue on Feb 28, 2026
  9. added 2 commits that reference this issue on Apr 5, 2026
  10. added a commit that references this issue on Apr 6, 2026
  11. eendebakpt commented on Apr 15, 2026

    @eendebakpt
    Contributor

    Some additional ideas for further improvements #128956 (comment)

  12. added a commit that references this issue on Apr 16, 2026
  13. added a commit that references this issue on Apr 20, 2026
  14. added 3 commits that reference this issue on Apr 25, 2026
  15. added a commit that references this issue on May 6, 2026
  16. added a commit that references this issue on May 8, 2026
  17. added a commit that references this issue on May 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

interpreter-core(Objects, Python, Grammar, and Parser dirs)performancePerformance or resource usagetype-featureA feature request or enhancement

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions