Skip to content

bpo-45510: Specialize BINARY_SUBTRACT #29523

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

Merged
merged 2 commits into from
Nov 18, 2021
Merged
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_long.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,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
68 changes: 35 additions & 33 deletions Include/opcode.h

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

2 changes: 2 additions & 0 deletions Lib/opcode.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,8 @@ def jabs_op(name, op):
"BINARY_OP_INPLACE_ADD_UNICODE",
"BINARY_OP_MULTIPLY_INT",
"BINARY_OP_MULTIPLY_FLOAT",
"BINARY_OP_SUBTRACT_INT",
"BINARY_OP_SUBTRACT_FLOAT",
"BINARY_SUBSCR_ADAPTIVE",
"BINARY_SUBSCR_LIST_INT",
"BINARY_SUBSCR_TUPLE_INT",
Expand Down
14 changes: 9 additions & 5 deletions Objects/longobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -3155,14 +3155,11 @@ 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;

CHECK_BINOP(a, b);

if (IS_MEDIUM_VALUE(a) && IS_MEDIUM_VALUE(b)) {
return _PyLong_FromSTwoDigits(medium_value(a) - medium_value(b));
}
Expand All @@ -3187,6 +3184,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
35 changes: 35 additions & 0 deletions Python/ceval.c
Original file line number Diff line number Diff line change
Expand Up @@ -1955,6 +1955,41 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, InterpreterFrame *frame, int thr
DISPATCH();
}

TARGET(BINARY_OP_SUBTRACT_INT) {
PyObject *left = SECOND();
PyObject *right = TOP();
DEOPT_IF(!PyLong_CheckExact(left), BINARY_OP);
DEOPT_IF(!PyLong_CheckExact(right), BINARY_OP);
STAT_INC(BINARY_OP, 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_OP_SUBTRACT_FLOAT) {
PyObject *left = SECOND();
PyObject *right = TOP();
DEOPT_IF(!PyFloat_CheckExact(left), BINARY_OP);
DEOPT_IF(!PyFloat_CheckExact(right), BINARY_OP);
STAT_INC(BINARY_OP, 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();
}

TARGET(BINARY_OP_ADD_UNICODE) {
PyObject *left = SECOND();
PyObject *right = TOP();
Expand Down
24 changes: 12 additions & 12 deletions Python/opcode_targets.h

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

13 changes: 13 additions & 0 deletions Python/specialize.c
Original file line number Diff line number Diff line change
Expand Up @@ -1420,6 +1420,19 @@ _Py_Specialize_BinaryOp(PyObject *lhs, PyObject *rhs, _Py_CODEUNIT *instr,
goto success;
}
break;
case NB_SUBTRACT:
case NB_INPLACE_SUBTRACT:
if (PyLong_CheckExact(lhs)) {
*instr = _Py_MAKECODEUNIT(BINARY_OP_SUBTRACT_INT,
_Py_OPARG(*instr));
goto success;
}
if (PyFloat_CheckExact(lhs)) {
*instr = _Py_MAKECODEUNIT(BINARY_OP_SUBTRACT_FLOAT,
_Py_OPARG(*instr));
goto success;
}
break;
default:
// These operators don't have any available specializations. Rather
// than repeatedly attempting to specialize them, just convert them
Expand Down