Skip to content

Commit

Permalink
pythongh-104028: Reduce objects creation while calling callback funct…
Browse files Browse the repository at this point in the history
…ion on gc
  • Loading branch information
corona10 committed May 1, 2023
1 parent 93107aa commit 47534f3
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Reduce objects creation while calling callback function on gc. Patch by
Dong-hee Na.
6 changes: 5 additions & 1 deletion Modules/gcmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -1388,10 +1388,13 @@ invoke_gc_callback(PyThreadState *tstate, const char *phase,
return;
}
}

PyObject *phase_obj = PyUnicode_FromString(phase);
PyObject *stack[] = {phase_obj, info};
for (Py_ssize_t i=0; i<PyList_GET_SIZE(gcstate->callbacks); i++) {
PyObject *r, *cb = PyList_GET_ITEM(gcstate->callbacks, i);
Py_INCREF(cb); /* make sure cb doesn't go away */
r = PyObject_CallFunction(cb, "sO", phase, info);
r = PyObject_Vectorcall(cb, stack, 2, NULL);
if (r == NULL) {
PyErr_WriteUnraisable(cb);
}
Expand All @@ -1400,6 +1403,7 @@ invoke_gc_callback(PyThreadState *tstate, const char *phase,
}
Py_DECREF(cb);
}
Py_DECREF(phase_obj);
Py_XDECREF(info);
assert(!_PyErr_Occurred(tstate));
}
Expand Down

0 comments on commit 47534f3

Please sign in to comment.