mirror of
https://github.com/python/cpython.git
synced 2025-08-23 10:16:01 +00:00
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
This commit is contained in:
parent
bbd3300ae8
commit
f49a1df6f3
6 changed files with 305 additions and 265 deletions
132
Python/executor_cases.c.h
generated
132
Python/executor_cases.c.h
generated
|
@ -943,8 +943,8 @@
|
|||
assert(WITHIN_STACK_BOUNDS());
|
||||
_PyFrame_SetStackPointer(frame, stack_pointer);
|
||||
res_o = PyObject_GetItem(PyStackRef_AsPyObjectBorrow(container), slice);
|
||||
stack_pointer = _PyFrame_GetStackPointer(frame);
|
||||
Py_DECREF(slice);
|
||||
stack_pointer = _PyFrame_GetStackPointer(frame);
|
||||
stack_pointer += 2;
|
||||
assert(WITHIN_STACK_BOUNDS());
|
||||
}
|
||||
|
@ -979,8 +979,8 @@
|
|||
assert(WITHIN_STACK_BOUNDS());
|
||||
_PyFrame_SetStackPointer(frame, stack_pointer);
|
||||
err = PyObject_SetItem(PyStackRef_AsPyObjectBorrow(container), slice, PyStackRef_AsPyObjectBorrow(v));
|
||||
stack_pointer = _PyFrame_GetStackPointer(frame);
|
||||
Py_DECREF(slice);
|
||||
stack_pointer = _PyFrame_GetStackPointer(frame);
|
||||
stack_pointer += 2;
|
||||
assert(WITHIN_STACK_BOUNDS());
|
||||
}
|
||||
|
@ -1300,11 +1300,13 @@
|
|||
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);
|
||||
PyStackRef_CLOSE(list_st);
|
||||
stack_pointer += -3;
|
||||
assert(WITHIN_STACK_BOUNDS());
|
||||
_PyFrame_SetStackPointer(frame, stack_pointer);
|
||||
Py_DECREF(old_value);
|
||||
stack_pointer = _PyFrame_GetStackPointer(frame);
|
||||
break;
|
||||
}
|
||||
|
||||
|
@ -1452,8 +1454,8 @@
|
|||
"'async for' received an object from __aiter__ "
|
||||
"that does not implement __anext__: %.100s",
|
||||
Py_TYPE(iter_o)->tp_name);
|
||||
stack_pointer = _PyFrame_GetStackPointer(frame);
|
||||
Py_DECREF(iter_o);
|
||||
stack_pointer = _PyFrame_GetStackPointer(frame);
|
||||
if (true) JUMP_TO_ERROR();
|
||||
}
|
||||
iter = PyStackRef_FromPyObjectSteal(iter_o);
|
||||
|
@ -2092,7 +2094,9 @@
|
|||
stack_pointer = _PyFrame_GetStackPointer(frame);
|
||||
JUMP_TO_ERROR();
|
||||
}
|
||||
_PyFrame_SetStackPointer(frame, stack_pointer);
|
||||
Py_DECREF(oldobj);
|
||||
stack_pointer = _PyFrame_GetStackPointer(frame);
|
||||
break;
|
||||
}
|
||||
|
||||
|
@ -2302,10 +2306,16 @@
|
|||
err = PySet_Add(set_o, PyStackRef_AsPyObjectBorrow(values[i]));
|
||||
stack_pointer = _PyFrame_GetStackPointer(frame);
|
||||
}
|
||||
PyStackRef_CLOSE(values[i]);
|
||||
}
|
||||
for (int _i = oparg; --_i >= 0;) {
|
||||
PyStackRef_CLOSE(values[_i]);
|
||||
}
|
||||
if (err != 0) {
|
||||
stack_pointer += -oparg;
|
||||
assert(WITHIN_STACK_BOUNDS());
|
||||
_PyFrame_SetStackPointer(frame, stack_pointer);
|
||||
Py_DECREF(set_o);
|
||||
stack_pointer = _PyFrame_GetStackPointer(frame);
|
||||
if (true) JUMP_TO_ERROR();
|
||||
}
|
||||
set = PyStackRef_FromPyObjectSteal(set_o);
|
||||
|
@ -2367,12 +2377,14 @@
|
|||
_PyFrame_SetStackPointer(frame, stack_pointer);
|
||||
err = PyObject_SetItem(LOCALS(), &_Py_ID(__annotations__),
|
||||
ann_dict);
|
||||
stack_pointer = _PyFrame_GetStackPointer(frame);
|
||||
Py_DECREF(ann_dict);
|
||||
stack_pointer = _PyFrame_GetStackPointer(frame);
|
||||
if (err) JUMP_TO_ERROR();
|
||||
}
|
||||
else {
|
||||
_PyFrame_SetStackPointer(frame, stack_pointer);
|
||||
Py_DECREF(ann_dict);
|
||||
stack_pointer = _PyFrame_GetStackPointer(frame);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
@ -2992,10 +3004,12 @@
|
|||
_PyDictValues_AddToInsertionOrder(values, index);
|
||||
}
|
||||
UNLOCK_OBJECT(owner_o);
|
||||
Py_XDECREF(old_value);
|
||||
PyStackRef_CLOSE(owner);
|
||||
stack_pointer += -2;
|
||||
assert(WITHIN_STACK_BOUNDS());
|
||||
_PyFrame_SetStackPointer(frame, stack_pointer);
|
||||
Py_XDECREF(old_value);
|
||||
stack_pointer = _PyFrame_GetStackPointer(frame);
|
||||
break;
|
||||
}
|
||||
|
||||
|
@ -3059,11 +3073,13 @@
|
|||
UNLOCK_OBJECT(dict);
|
||||
// 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);
|
||||
stack_pointer += -2;
|
||||
assert(WITHIN_STACK_BOUNDS());
|
||||
_PyFrame_SetStackPointer(frame, stack_pointer);
|
||||
Py_XDECREF(old_value);
|
||||
stack_pointer = _PyFrame_GetStackPointer(frame);
|
||||
break;
|
||||
}
|
||||
|
||||
|
@ -3083,10 +3099,12 @@
|
|||
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);
|
||||
stack_pointer += -2;
|
||||
assert(WITHIN_STACK_BOUNDS());
|
||||
_PyFrame_SetStackPointer(frame, stack_pointer);
|
||||
Py_XDECREF(old_value);
|
||||
stack_pointer = _PyFrame_GetStackPointer(frame);
|
||||
break;
|
||||
}
|
||||
|
||||
|
@ -3111,8 +3129,8 @@
|
|||
assert(WITHIN_STACK_BOUNDS());
|
||||
_PyFrame_SetStackPointer(frame, stack_pointer);
|
||||
int res_bool = PyObject_IsTrue(res_o);
|
||||
stack_pointer = _PyFrame_GetStackPointer(frame);
|
||||
Py_DECREF(res_o);
|
||||
stack_pointer = _PyFrame_GetStackPointer(frame);
|
||||
if (res_bool < 0) JUMP_TO_ERROR();
|
||||
res = res_bool ? PyStackRef_True : PyStackRef_False;
|
||||
}
|
||||
|
@ -3859,7 +3877,9 @@
|
|||
tb = Py_None;
|
||||
}
|
||||
else {
|
||||
_PyFrame_SetStackPointer(frame, stack_pointer);
|
||||
Py_DECREF(tb);
|
||||
stack_pointer = _PyFrame_GetStackPointer(frame);
|
||||
}
|
||||
assert(PyStackRef_LongCheck(lasti));
|
||||
(void)lasti; // Shut up compiler warning if asserts are off
|
||||
|
@ -4206,12 +4226,13 @@
|
|||
#endif
|
||||
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)) {
|
||||
PyStackRef_CLOSE(callable[0]);
|
||||
PyStackRef_XCLOSE(self_or_null[0]);
|
||||
|
@ -4228,10 +4249,11 @@
|
|||
stack_pointer = _PyFrame_GetStackPointer(frame);
|
||||
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]);
|
||||
}
|
||||
PyStackRef_CLOSE(callable[0]);
|
||||
PyStackRef_XCLOSE(self_or_null[0]);
|
||||
for (int _i = oparg; --_i >= 0;) {
|
||||
PyStackRef_CLOSE(args[_i]);
|
||||
}
|
||||
if (res_o == NULL) JUMP_TO_ERROR();
|
||||
res = PyStackRef_FromPyObjectSteal(res_o);
|
||||
stack_pointer[-2 - oparg] = res;
|
||||
|
@ -4723,11 +4745,11 @@
|
|||
PyObject *res_o = tp->tp_vectorcall((PyObject *)tp, args_o, total_args, NULL);
|
||||
stack_pointer = _PyFrame_GetStackPointer(frame);
|
||||
STACKREFS_TO_PYOBJECTS_CLEANUP(args_o);
|
||||
/* Free the arguments. */
|
||||
for (int i = 0; i < total_args; i++) {
|
||||
PyStackRef_CLOSE(arguments[i]);
|
||||
}
|
||||
PyStackRef_CLOSE(callable[0]);
|
||||
PyStackRef_XCLOSE(self_or_null[0]);
|
||||
for (int _i = oparg; --_i >= 0;) {
|
||||
PyStackRef_CLOSE(args[_i]);
|
||||
}
|
||||
if (res_o == NULL) JUMP_TO_ERROR();
|
||||
res = PyStackRef_FromPyObjectSteal(res_o);
|
||||
stack_pointer[-2 - oparg] = res;
|
||||
|
@ -4833,11 +4855,11 @@
|
|||
stack_pointer = _PyFrame_GetStackPointer(frame);
|
||||
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]);
|
||||
}
|
||||
PyStackRef_CLOSE(callable[0]);
|
||||
PyStackRef_XCLOSE(self_or_null[0]);
|
||||
for (int _i = oparg; --_i >= 0;) {
|
||||
PyStackRef_CLOSE(args[_i]);
|
||||
}
|
||||
if (res_o == NULL) JUMP_TO_ERROR();
|
||||
res = PyStackRef_FromPyObjectSteal(res_o);
|
||||
stack_pointer[-2 - oparg] = res;
|
||||
|
@ -4858,8 +4880,9 @@
|
|||
/* Builtin METH_FASTCALL | METH_KEYWORDS functions */
|
||||
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++;
|
||||
}
|
||||
if (!PyCFunction_CheckExact(callable_o)) {
|
||||
|
@ -4871,13 +4894,13 @@
|
|||
JUMP_TO_JUMP_TARGET();
|
||||
}
|
||||
STAT_INC(CALL, hit);
|
||||
/* res = func(self, args, nargs, kwnames) */
|
||||
/* res = func(self, arguments, nargs, kwnames) */
|
||||
_PyFrame_SetStackPointer(frame, stack_pointer);
|
||||
PyCFunctionFastWithKeywords cfunc =
|
||||
(PyCFunctionFastWithKeywords)(void(*)(void))
|
||||
PyCFunction_GET_FUNCTION(callable_o);
|
||||
stack_pointer = _PyFrame_GetStackPointer(frame);
|
||||
STACKREFS_TO_PYOBJECTS(args, total_args, args_o);
|
||||
STACKREFS_TO_PYOBJECTS(arguments, total_args, args_o);
|
||||
if (CONVERSION_FAILED(args_o)) {
|
||||
PyStackRef_CLOSE(callable[0]);
|
||||
PyStackRef_XCLOSE(self_or_null[0]);
|
||||
|
@ -4891,11 +4914,11 @@
|
|||
stack_pointer = _PyFrame_GetStackPointer(frame);
|
||||
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]);
|
||||
}
|
||||
PyStackRef_CLOSE(callable[0]);
|
||||
PyStackRef_XCLOSE(self_or_null[0]);
|
||||
for (int _i = oparg; --_i >= 0;) {
|
||||
PyStackRef_CLOSE(args[_i]);
|
||||
}
|
||||
if (res_o == NULL) JUMP_TO_ERROR();
|
||||
res = PyStackRef_FromPyObjectSteal(res_o);
|
||||
stack_pointer[-2 - oparg] = res;
|
||||
|
@ -5115,8 +5138,9 @@
|
|||
callable = &stack_pointer[-2 - oparg];
|
||||
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;
|
||||
|
@ -5130,14 +5154,14 @@
|
|||
JUMP_TO_JUMP_TARGET();
|
||||
}
|
||||
PyTypeObject *d_type = method->d_common.d_type;
|
||||
PyObject *self = PyStackRef_AsPyObjectBorrow(args[0]);
|
||||
PyObject *self = PyStackRef_AsPyObjectBorrow(arguments[0]);
|
||||
if (!Py_IS_TYPE(self, d_type)) {
|
||||
UOP_STAT_INC(uopcode, miss);
|
||||
JUMP_TO_JUMP_TARGET();
|
||||
}
|
||||
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)) {
|
||||
PyStackRef_CLOSE(callable[0]);
|
||||
PyStackRef_XCLOSE(self_or_null[0]);
|
||||
|
@ -5153,11 +5177,11 @@
|
|||
stack_pointer = _PyFrame_GetStackPointer(frame);
|
||||
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]);
|
||||
}
|
||||
PyStackRef_CLOSE(callable[0]);
|
||||
PyStackRef_XCLOSE(self_or_null[0]);
|
||||
for (int _i = oparg; --_i >= 0;) {
|
||||
PyStackRef_CLOSE(args[_i]);
|
||||
}
|
||||
if (res_o == NULL) JUMP_TO_ERROR();
|
||||
res = PyStackRef_FromPyObjectSteal(res_o);
|
||||
stack_pointer[-2 - oparg] = res;
|
||||
|
@ -5236,8 +5260,9 @@
|
|||
callable = &stack_pointer[-2 - oparg];
|
||||
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;
|
||||
|
@ -5251,14 +5276,14 @@
|
|||
UOP_STAT_INC(uopcode, miss);
|
||||
JUMP_TO_JUMP_TARGET();
|
||||
}
|
||||
PyObject *self = PyStackRef_AsPyObjectBorrow(args[0]);
|
||||
PyObject *self = PyStackRef_AsPyObjectBorrow(arguments[0]);
|
||||
if (!Py_IS_TYPE(self, method->d_common.d_type)) {
|
||||
UOP_STAT_INC(uopcode, miss);
|
||||
JUMP_TO_JUMP_TARGET();
|
||||
}
|
||||
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)) {
|
||||
PyStackRef_CLOSE(callable[0]);
|
||||
PyStackRef_XCLOSE(self_or_null[0]);
|
||||
|
@ -5274,11 +5299,11 @@
|
|||
stack_pointer = _PyFrame_GetStackPointer(frame);
|
||||
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]);
|
||||
}
|
||||
PyStackRef_CLOSE(callable[0]);
|
||||
PyStackRef_XCLOSE(self_or_null[0]);
|
||||
for (int _i = oparg; --_i >= 0;) {
|
||||
PyStackRef_CLOSE(args[_i]);
|
||||
}
|
||||
if (res_o == NULL) JUMP_TO_ERROR();
|
||||
res = PyStackRef_FromPyObjectSteal(res_o);
|
||||
stack_pointer[-2 - oparg] = res;
|
||||
|
@ -5334,8 +5359,9 @@
|
|||
PyObject *callable_o = PyStackRef_AsPyObjectBorrow(callable[0]);
|
||||
// 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);
|
||||
|
@ -5346,7 +5372,7 @@
|
|||
_PyFrame_SetStackPointer(frame, stack_pointer);
|
||||
_PyInterpreterFrame *temp = _PyEvalFramePushAndInit(
|
||||
tstate, callable[0], locals,
|
||||
args, positional_args, kwnames_o, frame
|
||||
arguments, positional_args, kwnames_o, frame
|
||||
);
|
||||
stack_pointer = _PyFrame_GetStackPointer(frame);
|
||||
PyStackRef_CLOSE(kwnames);
|
||||
|
@ -5463,12 +5489,13 @@
|
|||
#endif
|
||||
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)) {
|
||||
PyStackRef_CLOSE(callable[0]);
|
||||
PyStackRef_XCLOSE(self_or_null[0]);
|
||||
|
@ -5489,10 +5516,11 @@
|
|||
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]);
|
||||
}
|
||||
PyStackRef_CLOSE(callable[0]);
|
||||
PyStackRef_XCLOSE(self_or_null[0]);
|
||||
for (int _i = oparg; --_i >= 0;) {
|
||||
PyStackRef_CLOSE(args[_i]);
|
||||
}
|
||||
if (res_o == NULL) JUMP_TO_ERROR();
|
||||
res = PyStackRef_FromPyObjectSteal(res_o);
|
||||
stack_pointer[-3 - oparg] = res;
|
||||
|
@ -6128,7 +6156,9 @@
|
|||
|
||||
case _START_EXECUTOR: {
|
||||
PyObject *executor = (PyObject *)CURRENT_OPERAND0();
|
||||
_PyFrame_SetStackPointer(frame, stack_pointer);
|
||||
Py_DECREF(tstate->previous_executor);
|
||||
stack_pointer = _PyFrame_GetStackPointer(frame);
|
||||
tstate->previous_executor = NULL;
|
||||
#ifndef _Py_JIT
|
||||
current_executor = (_PyExecutorObject*)executor;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue