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.
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.
Activity
pythongh-100239: specialize long tail of binary operations
pythongh-100239: specialize long tail of binary operations
Merge remote-tracking branch 'upstream/main' into pythongh-100239
Merge remote-tracking branch 'upstream/main' into pythongh-100239
Merge remote-tracking branch 'upstream/main' into pythongh-100239
Merge branch 'main' into pythongh-100239
gh-100239: specialize long tail of binary operations (#128722)
pythongh-100239: specialize bitwose logical binary ops
pythongh-100239: specialize bitwise logical binary ops on non-negativ…
encukou commented on 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 commented on Jan 17, 2025
I'll take care of it!
gh-100239: Handle NaN and zero division in guards for `BINARY_OP_EXTE…
17 remaining items
gh-100239: specialize BINARY_OP/SUBSCR for list-slice (#132626)
gh-100239: Use ``PyFloat_AS_DOUBLE`` and `_PyLong_IsZero`` in the flo…
pythongh-100239: Use ``PyFloat_AS_DOUBLE`` and `_PyLong_IsZero`` in t…
pythongh-100239: Propagate type info through _BINARY_OP_EXTEND in tier 2
pythongh-100239: Propagate type info through _BINARY_OP_EXTEND in tier 2
gh-100239: Propagate type info through _BINARY_OP_EXTEND in tier 2 (G…
eendebakpt commented on Apr 15, 2026
Some additional ideas for further improvements #128956 (comment)
gh-100239: Specialize more binary operations using BINARY_OP_EXTEND (G…
pythongh-100239: expose `sq_repeat` helpers for `BINARY_OP_EXTEND`
pythongh-100239: Use ``PyFloat_AS_DOUBLE`` and `_PyLong_IsZero`` in t…
pythongh-100239: Propagate type info through _BINARY_OP_EXTEND in tie…
pythongh-100239: Specialize more binary operations using BINARY_OP_EX…
gh-100239: specialize mixed int/float inplace binary ops (GH-149413)
Merge branch 'main' into pythongh-100239/expose-sq-repeat-helpers
gh-100239: expose `sq_repeat` helpers for `BINARY_OP_EXTEND` (#148791)