Skip to content

bpo-45510: Specialize BINARY_SUBTRACT #29010

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

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Include/internal/pycore_code.h
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,7 @@ int _Py_Specialize_LoadMethod(PyObject *owner, _Py_CODEUNIT *instr, PyObject *na
int _Py_Specialize_BinarySubscr(PyObject *sub, PyObject *container, _Py_CODEUNIT *instr);
int _Py_Specialize_BinaryAdd(PyObject *left, PyObject *right, _Py_CODEUNIT *instr);
int _Py_Specialize_BinaryMultiply(PyObject *left, PyObject *right, _Py_CODEUNIT *instr);
int _Py_Specialize_BinarySubtract(PyObject *left, PyObject *right, _Py_CODEUNIT *instr);
int _Py_Specialize_CallFunction(PyObject *callable, _Py_CODEUNIT *instr, int nargs, SpecializedCacheEntry *cache, PyObject *builtins);

#define PRINT_SPECIALIZATION_STATS 0
Expand Down
1 change: 1 addition & 0 deletions Include/internal/pycore_long.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ static inline PyObject* _PyLong_GetOne(void)

PyObject *_PyLong_Add(PyLongObject *left, PyLongObject *right);
PyObject *_PyLong_Multiply(PyLongObject *left, PyLongObject *right);
PyObject *_PyLong_Subtract(PyLongObject *left, PyLongObject *right);

/* Used by Python/mystrtoul.c, _PyBytes_FromHex(),
_PyBytes_DecodeEscape(), etc. */
Expand Down
75 changes: 39 additions & 36 deletions Include/opcode.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Lib/opcode.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,9 @@ def jabs_op(name, op):
"BINARY_ADD_FLOAT",
"BINARY_ADD_UNICODE",
"BINARY_ADD_UNICODE_INPLACE_FAST",
"BINARY_SUBTRACT_ADAPTIVE",
"BINARY_SUBTRACT_INT",
"BINARY_SUBTRACT_FLOAT",
"BINARY_MULTIPLY_ADAPTIVE",
"BINARY_MULTIPLY_INT",
"BINARY_MULTIPLY_FLOAT",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Specialized the ``BINARY_SUBTRACT`` opcode to ``BINARY_SUBTRACT_INT`` and
``BINARY_SUBTRACT_FLOAT`` using the :pep:`659` machinery.
12 changes: 9 additions & 3 deletions Objects/longobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -3158,9 +3158,8 @@ long_add(PyLongObject *a, PyLongObject *b)
return _PyLong_Add(a, b);
}


static PyObject *
long_sub(PyLongObject *a, PyLongObject *b)
PyObject *
_PyLong_Subtract(PyLongObject *a, PyLongObject *b)
{
PyLongObject *z;

Expand Down Expand Up @@ -3190,6 +3189,13 @@ long_sub(PyLongObject *a, PyLongObject *b)
return (PyObject *)z;
}

static PyObject *
long_sub(PyLongObject *a, PyLongObject *b)
{
CHECK_BINOP(a, b);
return _PyLong_Subtract(a, b);
}

/* Grade school multiplication, ignoring the signs.
* Returns the absolute value of the product, or NULL if error.
*/
Expand Down
59 changes: 58 additions & 1 deletion Python/ceval.c
Original file line number Diff line number Diff line change
Expand Up @@ -2171,14 +2171,70 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, InterpreterFrame *frame, int thr
}

TARGET(BINARY_SUBTRACT) {
PREDICTED(BINARY_SUBTRACT);
STAT_INC(BINARY_SUBTRACT, unquickened);
PyObject *right = POP();
PyObject *left = TOP();
PyObject *diff = PyNumber_Subtract(left, right);
Py_DECREF(right);
Py_DECREF(left);
SET_TOP(diff);
if (diff == NULL)
if (diff == NULL) {
goto error;
}
DISPATCH();
}

TARGET(BINARY_SUBTRACT_ADAPTIVE) {
if (oparg == 0) {
PyObject *left = SECOND();
PyObject *right = TOP();
next_instr--;
if (_Py_Specialize_BinarySubtract(left, right, next_instr) < 0) {
goto error;
}
DISPATCH();
}
else {
STAT_INC(BINARY_SUBTRACT, deferred);
UPDATE_PREV_INSTR_OPARG(next_instr, oparg - 1);
STAT_DEC(BINARY_SUBTRACT, unquickened);
JUMP_TO_INSTRUCTION(BINARY_SUBTRACT);
}
}

TARGET(BINARY_SUBTRACT_INT) {
PyObject *left = SECOND();
PyObject *right = TOP();
DEOPT_IF(!PyLong_CheckExact(left), BINARY_SUBTRACT);
DEOPT_IF(!PyLong_CheckExact(right), BINARY_SUBTRACT);
STAT_INC(BINARY_SUBTRACT, hit);
PyObject *sub = _PyLong_Subtract((PyLongObject *)left, (PyLongObject *)right);
SET_SECOND(sub);
Py_DECREF(right);
Py_DECREF(left);
STACK_SHRINK(1);
if (sub == NULL) {
goto error;
}
DISPATCH();
}

TARGET(BINARY_SUBTRACT_FLOAT) {
PyObject *left = SECOND();
PyObject *right = TOP();
DEOPT_IF(!PyFloat_CheckExact(left), BINARY_SUBTRACT);
DEOPT_IF(!PyFloat_CheckExact(right), BINARY_SUBTRACT);
STAT_INC(BINARY_SUBTRACT, hit);
double dsub = ((PyFloatObject *)left)->ob_fval - ((PyFloatObject *)right)->ob_fval;
PyObject *sub = PyFloat_FromDouble(dsub);
SET_SECOND(sub);
Py_DECREF(right);
Py_DECREF(left);
STACK_SHRINK(1);
if (sub == NULL) {
goto error;
}
DISPATCH();
}

Expand Down Expand Up @@ -5135,6 +5191,7 @@ MISS_WITH_CACHE(CALL_FUNCTION)
MISS_WITH_OPARG_COUNTER(BINARY_SUBSCR)
MISS_WITH_OPARG_COUNTER(BINARY_ADD)
MISS_WITH_OPARG_COUNTER(BINARY_MULTIPLY)
MISS_WITH_OPARG_COUNTER(BINARY_SUBTRACT)

binary_subscr_dict_error:
{
Expand Down
54 changes: 27 additions & 27 deletions Python/opcode_targets.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading