aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/Python/bytecodes.c
diff options
context:
space:
mode:
authorMark Shannon <mark@hotpy.org>2025-01-14 15:08:56 +0000
committerGitHub <noreply@github.com>2025-01-14 15:08:56 +0000
commitf49a1df6f3b1249bcaedfda8e000e27c6cdffd99 (patch)
tree154fa92c820b5d06bee762eb5778f8f1556318ba /Python/bytecodes.c
parentbbd3300ae82a71da483dbb0c345175ba090263c4 (diff)
downloadcpython-f49a1df6f3b1249bcaedfda8e000e27c6cdffd99.tar.gz
cpython-f49a1df6f3b1249bcaedfda8e000e27c6cdffd99.zip
GH-128682: Convert explicit loops closing arrays into `DECREF_INPUTS`. (GH-128822)
* Mark Py_DECREF and Py_XDECREF as escaping * Remove explicit loops for clearing array inputs
Diffstat (limited to 'Python/bytecodes.c')
-rw-r--r--Python/bytecodes.c139
1 files changed, 46 insertions, 93 deletions
diff --git a/Python/bytecodes.c b/Python/bytecodes.c
index ef7086c3c02..cec530fefff 100644
--- a/Python/bytecodes.c
+++ b/Python/bytecodes.c
@@ -1004,10 +1004,10 @@ dummy_func(
PyList_SET_ITEM(list, index, PyStackRef_AsPyObjectSteal(value));
assert(old_value != NULL);
UNLOCK_OBJECT(list); // unlock before decrefs!
- Py_DECREF(old_value);
PyStackRef_CLOSE_SPECIALIZED(sub_st, _PyLong_ExactDealloc);
DEAD(sub_st);
PyStackRef_CLOSE(list_st);
+ Py_DECREF(old_value);
}
inst(STORE_SUBSCR_DICT, (unused/1, value, dict_st, sub -- )) {
@@ -1885,9 +1885,8 @@ dummy_func(
if (err == 0) {
err = PySet_Add(set_o, PyStackRef_AsPyObjectBorrow(values[i]));
}
- PyStackRef_CLOSE(values[i]);
}
- DEAD(values);
+ DECREF_INPUTS();
if (err != 0) {
Py_DECREF(set_o);
ERROR_IF(true, error);
@@ -2412,8 +2411,8 @@ dummy_func(
_PyDictValues_AddToInsertionOrder(values, index);
}
UNLOCK_OBJECT(owner_o);
- Py_XDECREF(old_value);
PyStackRef_CLOSE(owner);
+ Py_XDECREF(old_value);
}
macro(STORE_ATTR_INSTANCE_VALUE) =
@@ -2457,9 +2456,9 @@ dummy_func(
// old_value should be DECREFed after GC track checking is done, if not, it could raise a segmentation fault,
// when dict only holds the strong reference to value in ep->me_value.
- Py_XDECREF(old_value);
STAT_INC(STORE_ATTR, hit);
PyStackRef_CLOSE(owner);
+ Py_XDECREF(old_value);
}
macro(STORE_ATTR_WITH_HINT) =
@@ -2476,8 +2475,8 @@ dummy_func(
PyObject *old_value = *(PyObject **)addr;
FT_ATOMIC_STORE_PTR_RELEASE(*(PyObject **)addr, PyStackRef_AsPyObjectSteal(value));
UNLOCK_OBJECT(owner_o);
- Py_XDECREF(old_value);
PyStackRef_CLOSE(owner);
+ Py_XDECREF(old_value);
}
macro(STORE_ATTR_SLOT) =
@@ -3435,8 +3434,9 @@ dummy_func(
// oparg counts all of the args, but *not* self:
int total_args = oparg;
+ _PyStackRef *arguments = args;
if (!PyStackRef_IsNull(self_or_null[0])) {
- args--;
+ arguments--;
total_args++;
}
// Check if the call can be inlined or not
@@ -3448,7 +3448,7 @@ dummy_func(
PyObject *locals = code_flags & CO_OPTIMIZED ? NULL : Py_NewRef(PyFunction_GET_GLOBALS(callable_o));
_PyInterpreterFrame *new_frame = _PyEvalFramePushAndInit(
tstate, callable[0], locals,
- args, total_args, NULL, frame
+ arguments, total_args, NULL, frame
);
// Manipulate stack directly since we leave using DISPATCH_INLINED().
SYNC_SP();
@@ -3461,13 +3461,9 @@ dummy_func(
DISPATCH_INLINED(new_frame);
}
/* Callable is not a normal Python function */
- STACKREFS_TO_PYOBJECTS(args, total_args, args_o);
+ STACKREFS_TO_PYOBJECTS(arguments, total_args, args_o);
if (CONVERSION_FAILED(args_o)) {
- for (int i = 0; i < total_args; i++) {
- PyStackRef_CLOSE(args[i]);
- }
- DEAD(self_or_null);
- PyStackRef_CLOSE(callable[0]);
+ DECREF_INPUTS();
ERROR_IF(true, error);
}
PyObject *res_o = PyObject_Vectorcall(
@@ -3477,7 +3473,7 @@ dummy_func(
STACKREFS_TO_PYOBJECTS_CLEANUP(args_o);
if (opcode == INSTRUMENTED_CALL) {
PyObject *arg = total_args == 0 ?
- &_PyInstrumentation_MISSING : PyStackRef_AsPyObjectBorrow(args[0]);
+ &_PyInstrumentation_MISSING : PyStackRef_AsPyObjectBorrow(arguments[0]);
if (res_o == NULL) {
_Py_call_instrumentation_exc2(
tstate, PY_MONITORING_EVENT_C_RAISE,
@@ -3493,11 +3489,7 @@ dummy_func(
}
}
assert((res_o != NULL) ^ (_PyErr_Occurred(tstate) != NULL));
- for (int i = 0; i < total_args; i++) {
- PyStackRef_CLOSE(args[i]);
- }
- DEAD(self_or_null);
- PyStackRef_CLOSE(callable[0]);
+ DECREF_INPUTS();
ERROR_IF(res_o == NULL, error);
res = PyStackRef_FromPyObjectSteal(res_o);
}
@@ -3617,12 +3609,13 @@ dummy_func(
PyObject *callable_o = PyStackRef_AsPyObjectBorrow(callable[0]);
int total_args = oparg;
+ _PyStackRef *arguments = args;
if (!PyStackRef_IsNull(self_or_null[0])) {
- args--;
+ arguments--;
total_args++;
}
/* Callable is not a normal Python function */
- STACKREFS_TO_PYOBJECTS(args, total_args, args_o);
+ STACKREFS_TO_PYOBJECTS(arguments, total_args, args_o);
if (CONVERSION_FAILED(args_o)) {
DECREF_INPUTS();
ERROR_IF(true, error);
@@ -3633,11 +3626,7 @@ dummy_func(
NULL);
STACKREFS_TO_PYOBJECTS_CLEANUP(args_o);
assert((res_o != NULL) ^ (_PyErr_Occurred(tstate) != NULL));
- for (int i = 0; i < total_args; i++) {
- PyStackRef_CLOSE(args[i]);
- }
- DEAD(self_or_null);
- PyStackRef_CLOSE(callable[0]);
+ DECREF_INPUTS();
ERROR_IF(res_o == NULL, error);
res = PyStackRef_FromPyObjectSteal(res_o);
}
@@ -3877,15 +3866,9 @@ dummy_func(
DECREF_INPUTS();
ERROR_IF(true, error);
}
- DEAD(self_or_null);
PyObject *res_o = tp->tp_vectorcall((PyObject *)tp, args_o, total_args, NULL);
STACKREFS_TO_PYOBJECTS_CLEANUP(args_o);
- /* Free the arguments. */
- for (int i = 0; i < total_args; i++) {
- PyStackRef_CLOSE(arguments[i]);
- }
- DEAD(args);
- PyStackRef_CLOSE(callable[0]);
+ DECREF_INPUTS();
ERROR_IF(res_o == NULL, error);
res = PyStackRef_FromPyObjectSteal(res_o);
}
@@ -3952,20 +3935,13 @@ dummy_func(
DECREF_INPUTS();
ERROR_IF(true, error);
}
- DEAD(self_or_null);
PyObject *res_o = ((PyCFunctionFast)(void(*)(void))cfunc)(
PyCFunction_GET_SELF(callable_o),
args_o,
total_args);
STACKREFS_TO_PYOBJECTS_CLEANUP(args_o);
assert((res_o != NULL) ^ (_PyErr_Occurred(tstate) != NULL));
-
- /* Free the arguments. */
- for (int i = 0; i < total_args; i++) {
- PyStackRef_CLOSE(arguments[i]);
- }
- DEAD(args);
- PyStackRef_CLOSE(callable[0]);
+ DECREF_INPUTS();
ERROR_IF(res_o == NULL, error);
res = PyStackRef_FromPyObjectSteal(res_o);
}
@@ -3981,34 +3957,28 @@ dummy_func(
PyObject *callable_o = PyStackRef_AsPyObjectBorrow(callable[0]);
int total_args = oparg;
+ _PyStackRef *arguments = args;
if (!PyStackRef_IsNull(self_or_null[0])) {
- args--;
+ arguments--;
total_args++;
}
DEOPT_IF(!PyCFunction_CheckExact(callable_o));
DEOPT_IF(PyCFunction_GET_FLAGS(callable_o) != (METH_FASTCALL | METH_KEYWORDS));
STAT_INC(CALL, hit);
- /* res = func(self, args, nargs, kwnames) */
+ /* res = func(self, arguments, nargs, kwnames) */
PyCFunctionFastWithKeywords cfunc =
(PyCFunctionFastWithKeywords)(void(*)(void))
PyCFunction_GET_FUNCTION(callable_o);
- STACKREFS_TO_PYOBJECTS(args, total_args, args_o);
+ STACKREFS_TO_PYOBJECTS(arguments, total_args, args_o);
if (CONVERSION_FAILED(args_o)) {
DECREF_INPUTS();
ERROR_IF(true, error);
}
PyObject *res_o = cfunc(PyCFunction_GET_SELF(callable_o), args_o, total_args, NULL);
STACKREFS_TO_PYOBJECTS_CLEANUP(args_o);
-
assert((res_o != NULL) ^ (_PyErr_Occurred(tstate) != NULL));
-
- /* Free the arguments. */
- for (int i = 0; i < total_args; i++) {
- PyStackRef_CLOSE(args[i]);
- }
- DEAD(self_or_null);
- PyStackRef_CLOSE(callable[0]);
+ DECREF_INPUTS();
ERROR_IF(res_o == NULL, error);
res = PyStackRef_FromPyObjectSteal(res_o);
}
@@ -4147,8 +4117,9 @@ dummy_func(
PyObject *callable_o = PyStackRef_AsPyObjectBorrow(callable[0]);
int total_args = oparg;
+ _PyStackRef *arguments = args;
if (!PyStackRef_IsNull(self_or_null[0])) {
- args--;
+ arguments--;
total_args++;
}
PyMethodDescrObject *method = (PyMethodDescrObject *)callable_o;
@@ -4156,12 +4127,12 @@ dummy_func(
PyMethodDef *meth = method->d_method;
EXIT_IF(meth->ml_flags != (METH_FASTCALL|METH_KEYWORDS));
PyTypeObject *d_type = method->d_common.d_type;
- PyObject *self = PyStackRef_AsPyObjectBorrow(args[0]);
+ PyObject *self = PyStackRef_AsPyObjectBorrow(arguments[0]);
EXIT_IF(!Py_IS_TYPE(self, d_type));
STAT_INC(CALL, hit);
int nargs = total_args - 1;
- STACKREFS_TO_PYOBJECTS(args, total_args, args_o);
+ STACKREFS_TO_PYOBJECTS(arguments, total_args, args_o);
if (CONVERSION_FAILED(args_o)) {
DECREF_INPUTS();
ERROR_IF(true, error);
@@ -4171,13 +4142,7 @@ dummy_func(
PyObject *res_o = cfunc(self, (args_o + 1), nargs, NULL);
STACKREFS_TO_PYOBJECTS_CLEANUP(args_o);
assert((res_o != NULL) ^ (_PyErr_Occurred(tstate) != NULL));
-
- /* Free the arguments. */
- for (int i = 0; i < total_args; i++) {
- PyStackRef_CLOSE(args[i]);
- }
- DEAD(self_or_null);
- PyStackRef_CLOSE(callable[0]);
+ DECREF_INPUTS();
ERROR_IF(res_o == NULL, error);
res = PyStackRef_FromPyObjectSteal(res_o);
}
@@ -4231,8 +4196,9 @@ dummy_func(
PyObject *callable_o = PyStackRef_AsPyObjectBorrow(callable[0]);
int total_args = oparg;
+ _PyStackRef *arguments = args;
if (!PyStackRef_IsNull(self_or_null[0])) {
- args--;
+ arguments--;
total_args++;
}
PyMethodDescrObject *method = (PyMethodDescrObject *)callable_o;
@@ -4240,12 +4206,12 @@ dummy_func(
EXIT_IF(!Py_IS_TYPE(method, &PyMethodDescr_Type));
PyMethodDef *meth = method->d_method;
EXIT_IF(meth->ml_flags != METH_FASTCALL);
- PyObject *self = PyStackRef_AsPyObjectBorrow(args[0]);
+ PyObject *self = PyStackRef_AsPyObjectBorrow(arguments[0]);
EXIT_IF(!Py_IS_TYPE(self, method->d_common.d_type));
STAT_INC(CALL, hit);
int nargs = total_args - 1;
- STACKREFS_TO_PYOBJECTS(args, total_args, args_o);
+ STACKREFS_TO_PYOBJECTS(arguments, total_args, args_o);
if (CONVERSION_FAILED(args_o)) {
DECREF_INPUTS();
ERROR_IF(true, error);
@@ -4255,13 +4221,7 @@ dummy_func(
PyObject *res_o = cfunc(self, (args_o + 1), nargs);
STACKREFS_TO_PYOBJECTS_CLEANUP(args_o);
assert((res_o != NULL) ^ (_PyErr_Occurred(tstate) != NULL));
-
- /* Clear the stack of the arguments. */
- for (int i = 0; i < total_args; i++) {
- PyStackRef_CLOSE(args[i]);
- }
- DEAD(self_or_null);
- PyStackRef_CLOSE(callable[0]);
+ DECREF_INPUTS();
ERROR_IF(res_o == NULL, error);
res = PyStackRef_FromPyObjectSteal(res_o);
}
@@ -4313,8 +4273,9 @@ dummy_func(
// oparg counts all of the args, but *not* self:
int total_args = oparg;
+ _PyStackRef *arguments = args;
if (!PyStackRef_IsNull(self_or_null[0])) {
- args--;
+ arguments--;
total_args++;
}
int positional_args = total_args - (int)PyTuple_GET_SIZE(kwnames_o);
@@ -4327,7 +4288,7 @@ dummy_func(
PyObject *locals = code_flags & CO_OPTIMIZED ? NULL : Py_NewRef(PyFunction_GET_GLOBALS(callable_o));
_PyInterpreterFrame *new_frame = _PyEvalFramePushAndInit(
tstate, callable[0], locals,
- args, positional_args, kwnames_o, frame
+ arguments, positional_args, kwnames_o, frame
);
PyStackRef_CLOSE(kwnames);
// Sync stack explicitly since we leave using DISPATCH_INLINED().
@@ -4342,7 +4303,7 @@ dummy_func(
DISPATCH_INLINED(new_frame);
}
/* Callable is not a normal Python function */
- STACKREFS_TO_PYOBJECTS(args, total_args, args_o);
+ STACKREFS_TO_PYOBJECTS(arguments, total_args, args_o);
if (CONVERSION_FAILED(args_o)) {
DECREF_INPUTS();
ERROR_IF(true, error);
@@ -4354,7 +4315,7 @@ dummy_func(
STACKREFS_TO_PYOBJECTS_CLEANUP(args_o);
if (opcode == INSTRUMENTED_CALL_KW) {
PyObject *arg = total_args == 0 ?
- &_PyInstrumentation_MISSING : PyStackRef_AsPyObjectBorrow(args[0]);
+ &_PyInstrumentation_MISSING : PyStackRef_AsPyObjectBorrow(arguments[0]);
if (res_o == NULL) {
_Py_call_instrumentation_exc2(
tstate, PY_MONITORING_EVENT_C_RAISE,
@@ -4369,13 +4330,7 @@ dummy_func(
}
}
}
- PyStackRef_CLOSE(kwnames);
- assert((res_o != NULL) ^ (_PyErr_Occurred(tstate) != NULL));
- for (int i = 0; i < total_args; i++) {
- PyStackRef_CLOSE(args[i]);
- }
- DEAD(self_or_null);
- PyStackRef_CLOSE(callable[0]);
+ DECREF_INPUTS();
ERROR_IF(res_o == NULL, error);
res = PyStackRef_FromPyObjectSteal(res_o);
}
@@ -4385,8 +4340,9 @@ dummy_func(
// oparg counts all of the args, but *not* self:
int total_args = oparg;
+ _PyStackRef *arguments = args;
if (!PyStackRef_IsNull(self_or_null[0])) {
- args--;
+ arguments--;
total_args++;
}
PyObject *kwnames_o = PyStackRef_AsPyObjectBorrow(kwnames);
@@ -4396,7 +4352,7 @@ dummy_func(
PyObject *locals = code_flags & CO_OPTIMIZED ? NULL : Py_NewRef(PyFunction_GET_GLOBALS(callable_o));
_PyInterpreterFrame *temp = _PyEvalFramePushAndInit(
tstate, callable[0], locals,
- args, positional_args, kwnames_o, frame
+ arguments, positional_args, kwnames_o, frame
);
PyStackRef_CLOSE(kwnames);
// The frame has stolen all the arguments from the stack,
@@ -4487,12 +4443,13 @@ dummy_func(
PyObject *callable_o = PyStackRef_AsPyObjectBorrow(callable[0]);
int total_args = oparg;
+ _PyStackRef *arguments = args;
if (!PyStackRef_IsNull(self_or_null[0])) {
- args--;
+ arguments--;
total_args++;
}
/* Callable is not a normal Python function */
- STACKREFS_TO_PYOBJECTS(args, total_args, args_o);
+ STACKREFS_TO_PYOBJECTS(arguments, total_args, args_o);
if (CONVERSION_FAILED(args_o)) {
DECREF_INPUTS();
ERROR_IF(true, error);
@@ -4506,11 +4463,7 @@ dummy_func(
PyStackRef_CLOSE(kwnames);
STACKREFS_TO_PYOBJECTS_CLEANUP(args_o);
assert((res_o != NULL) ^ (_PyErr_Occurred(tstate) != NULL));
- for (int i = 0; i < total_args; i++) {
- PyStackRef_CLOSE(args[i]);
- }
- DEAD(self_or_null);
- PyStackRef_CLOSE(callable[0]);
+ DECREF_INPUTS();
ERROR_IF(res_o == NULL, error);
res = PyStackRef_FromPyObjectSteal(res_o);
}