Skip to content

Commit

Permalink
GH-102300: Reuse objects with refcount == 1 in float specialized bina…
Browse files Browse the repository at this point in the history
…ry ops. (GH-102301)
  • Loading branch information
markshannon committed Mar 13, 2023
1 parent 78e4e6c commit 233e32f
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 24 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Reuse operands with refcount of 1 in float specializations of BINARY_OP.
15 changes: 3 additions & 12 deletions Python/bytecodes.c
Original file line number Diff line number Diff line change
Expand Up @@ -195,10 +195,7 @@ dummy_func(
STAT_INC(BINARY_OP, hit);
double dprod = ((PyFloatObject *)left)->ob_fval *
((PyFloatObject *)right)->ob_fval;
prod = PyFloat_FromDouble(dprod);
_Py_DECREF_SPECIALIZED(right, _PyFloat_ExactDealloc);
_Py_DECREF_SPECIALIZED(left, _PyFloat_ExactDealloc);
ERROR_IF(prod == NULL, error);
DECREF_INPUTS_AND_REUSE_FLOAT(left, right, dprod, prod);
}

inst(BINARY_OP_SUBTRACT_INT, (unused/1, left, right -- sub)) {
Expand All @@ -218,10 +215,7 @@ dummy_func(
DEOPT_IF(!PyFloat_CheckExact(right), BINARY_OP);
STAT_INC(BINARY_OP, hit);
double dsub = ((PyFloatObject *)left)->ob_fval - ((PyFloatObject *)right)->ob_fval;
sub = PyFloat_FromDouble(dsub);
_Py_DECREF_SPECIALIZED(right, _PyFloat_ExactDealloc);
_Py_DECREF_SPECIALIZED(left, _PyFloat_ExactDealloc);
ERROR_IF(sub == NULL, error);
DECREF_INPUTS_AND_REUSE_FLOAT(left, right, dsub, sub);
}

inst(BINARY_OP_ADD_UNICODE, (unused/1, left, right -- res)) {
Expand Down Expand Up @@ -278,10 +272,7 @@ dummy_func(
STAT_INC(BINARY_OP, hit);
double dsum = ((PyFloatObject *)left)->ob_fval +
((PyFloatObject *)right)->ob_fval;
sum = PyFloat_FromDouble(dsum);
_Py_DECREF_SPECIALIZED(right, _PyFloat_ExactDealloc);
_Py_DECREF_SPECIALIZED(left, _PyFloat_ExactDealloc);
ERROR_IF(sum == NULL, error);
DECREF_INPUTS_AND_REUSE_FLOAT(left, right, dsum, sum);
}

inst(BINARY_OP_ADD_INT, (unused/1, left, right -- sum)) {
Expand Down
20 changes: 20 additions & 0 deletions Python/ceval_macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -350,3 +350,23 @@ GETITEM(PyObject *v, Py_ssize_t i) {

#define KWNAMES_LEN() \
(kwnames == NULL ? 0 : ((int)PyTuple_GET_SIZE(kwnames)))

#define DECREF_INPUTS_AND_REUSE_FLOAT(left, right, dval, result) \
do { \
if (Py_REFCNT(left) == 1) { \
((PyFloatObject *)left)->ob_fval = (dval); \
_Py_DECREF_SPECIALIZED(right, _PyFloat_ExactDealloc);\
result = (left); \
} \
else if (Py_REFCNT(right) == 1) {\
((PyFloatObject *)right)->ob_fval = (dval); \
_Py_DECREF_NO_DEALLOC(left); \
result = (right); \
}\
else { \
result = PyFloat_FromDouble(dval); \
if ((result) == NULL) goto error; \
_Py_DECREF_NO_DEALLOC(left); \
_Py_DECREF_NO_DEALLOC(right); \
} \
} while (0)
15 changes: 3 additions & 12 deletions Python/generated_cases.c.h

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

0 comments on commit 233e32f

Please sign in to comment.