GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Specialize long tail of binary operations using a table. · Issue #100239 · python/cpython

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

New issue
Copy link
New issue
Copy link
Open
Open
Specialize long tail of binary operations using a table.#100239
Copy link
Assignees
brandtbucher
Labels
interpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)performancePerformance or resource usagePerformance or resource usagetype-featureA feature request or enhancementA feature request or enhancement

Description

@markshannon
markshannon
opened on Dec 14, 2022
Issue body actions

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

  • gh-100239: specialize long tail of binary operations #128722
  • gh-100239: specialize bitwise logical binary ops on ints #128927
  • gh-100239: Specialize binary operations using BINARY_OP_EXTEND #128956
  • gh-100239: Handle NaN and zero division in guards #128963
  • gh-100239: replace BINARY_SUBSCR & family by BINARY_OP with oparg NB_SUBSCR #129379
  • gh-100239: specialize left and right shift ops on ints #129431
  • gh-100239: replace BINARY_SUBSCR & family by BINARY_OP with oparg NB_SUBSCR #129700
  • gh-100239: more refined specialisation stats for BINARY_OP/SUBSCR #132068
  • gh-100239: fix bug in comparison #132093
  • gh-100239: more stats for BINARY_OP/SUBSCR specialization #132230
  • gh-100239: specialize dict subclasses with no getitem override #132383
  • gh-100239: specialize BINARY_OP/SUBSCR for list-slice #132626
  • gh-100239: use PyFloat_AS_DOUBLE and _PyLong_IsZero in the float / compactlong specializations #144826
  • gh-100239: Propagate type info through _BINARY_OP_EXTEND in tier 2 #148146
  • gh-100239: expose sq_repeat helpers for BINARY_OP_EXTEND #148791
  • gh-100239: specialize mixed int/float inplace binary ops #149413
  • gh-100239: specialize bytes binary operations #149458
  • gh-100239: Specialize exact float division with BINARY_OP_EXTEND #156324
  • gh-100239: specialize small integer powers (x**2, x**3) of compact ints #156918
Reacted by zhuofeng, Brandt Bucher, Dennis Sweeney, Donghee Na, Alexandr Artemyev and Wulian233

Activity

Next
  1. markshannon
    added
    performancePerformance or resource usagePerformance or resource usage
    on Dec 14, 2022
  2. markshannon
    assigned
    brandtbucher
    on Dec 14, 2022
  3. iritkatriel
    added
    interpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)
    on Nov 28, 2023
  4. iritkatriel
    added 9 commits that reference this issue on Jan 10, 2025

    pythongh-100239: specialize long tail of binary operations

    8d8e4f0

    pythongh-100239: specialize long tail of binary operations

    7264e37

    Merge remote-tracking branch 'upstream/main' into pythongh-100239

    c74a196

    Merge remote-tracking branch 'upstream/main' into pythongh-100239

    68c4f34

    Merge remote-tracking branch 'upstream/main' into pythongh-100239

    cffb3d6

    Merge branch 'main' into pythongh-100239

    47395aa

    gh-100239: specialize long tail of binary operations (#128722)

    3893a92

    pythongh-100239: specialize bitwose logical binary ops

    7a6319b

    pythongh-100239: specialize bitwise logical binary ops on non-negativ…

    c6f2deb
  5. encukou commented on Jan 17, 2025

    @encukou
    encukou
    on Jan 17, 2025
    Member
    More actions

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

  6. Eclips4 commented on Jan 17, 2025

    @Eclips4
    Eclips4
    on Jan 17, 2025
    Member
    More actions

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

  7. Eclips4
    added a commit that references this issue on Jan 19, 2025

    gh-100239: Handle NaN and zero division in guards for `BINARY_OP_EXTE…

    6c52ada
  8. 19 remaining items

  9. brijkapadia
    added a commit that references this issue on Feb 28, 2026

    pythongh-100239: Use ``PyFloat_AS_DOUBLE`` and `_PyLong_IsZero`` in t…

    c155613
  10. eendebakpt
    added 2 commits that reference this issue on Apr 5, 2026

    pythongh-100239: Propagate type info through _BINARY_OP_EXTEND in tier 2

    060641a

    pythongh-100239: Propagate type info through _BINARY_OP_EXTEND in tier 2

    c42a7a8
  11. Fidget-Spinner
    added a commit that references this issue on Apr 6, 2026

    gh-100239: Propagate type info through _BINARY_OP_EXTEND in tier 2 (G…

    efda60e
  12. eendebakpt commented on Apr 15, 2026

    @eendebakpt
    eendebakpt
    on Apr 15, 2026
    Contributor
    More actions

    Some additional ideas for further improvements #128956 (comment)

  13. markshannon
    added a commit that references this issue on Apr 16, 2026

    gh-100239: Specialize more binary operations using BINARY_OP_EXTEND (G…

    1f6a09f
  14. NekoAsakura
    added a commit that references this issue on Apr 20, 2026

    pythongh-100239: expose `sq_repeat` helpers for `BINARY_OP_EXTEND`

    d8f65a1
  15. ljfp
    added 3 commits that reference this issue on Apr 25, 2026

    pythongh-100239: Use ``PyFloat_AS_DOUBLE`` and `_PyLong_IsZero`` in t…

    c33f46d

    pythongh-100239: Propagate type info through _BINARY_OP_EXTEND in tie…

    f7f40eb

    pythongh-100239: Specialize more binary operations using BINARY_OP_EX…

    d743931
  16. markshannon
    added a commit that references this issue on May 6, 2026

    gh-100239: specialize mixed int/float inplace binary ops (GH-149413)

    0b75b73
  17. NekoAsakura
    added a commit that references this issue on May 8, 2026

    Merge branch 'main' into pythongh-100239/expose-sq-repeat-helpers

    87fd3b4
  18. kumaraditya303
    added a commit that references this issue on May 8, 2026

    gh-100239: expose `sq_repeat` helpers for `BINARY_OP_EXTEND` (#148791)

    d2d24e4
  19. eendebakpt
    added 2 commits that reference this issue on Nov 3, 2026

    pythongh-100239: Instrument SWITCH_OP as its generic instruction

    cccb5fe

    pythongh-100239: Do the deallocation bookkeeping in _PyStolenTuple_Free

    5702435
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

  • @brandtbucher
    brandtbucher

Labels

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

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

    You are visitor 01402503 — GeezerVison does not store the pages you look up.
    Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.