From d55c1a193b732352a48bca0e37d5ec24bafc45bf Mon Sep 17 00:00:00 2001 From: Dong-hee Na Date: Mon, 1 May 2023 13:46:18 +0900 Subject: [PATCH 1/3] gh-104028: Reduce objects creation while calling callback function on gc --- .../2023-05-01-14-10-38.gh-issue-104028.dxfh13.rst | 2 ++ Modules/gcmodule.c | 6 +++++- 2 files changed, 7 insertions(+), 1 deletion(-) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2023-05-01-14-10-38.gh-issue-104028.dxfh13.rst diff --git a/Misc/NEWS.d/next/Core and Builtins/2023-05-01-14-10-38.gh-issue-104028.dxfh13.rst b/Misc/NEWS.d/next/Core and Builtins/2023-05-01-14-10-38.gh-issue-104028.dxfh13.rst new file mode 100644 index 00000000000000..01377d660ec23d --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2023-05-01-14-10-38.gh-issue-104028.dxfh13.rst @@ -0,0 +1,2 @@ +Reduce objects creation while calling callback function on gc. Patch by +Dong-hee Na. diff --git a/Modules/gcmodule.c b/Modules/gcmodule.c index 966c1e615502ef..19f65d1bf05a65 100644 --- a/Modules/gcmodule.c +++ b/Modules/gcmodule.c @@ -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; icallbacks); 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); } @@ -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)); } From 952674ff44b7a071a86cda1561072bd23556a91a Mon Sep 17 00:00:00 2001 From: Dong-hee Na Date: Mon, 1 May 2023 14:50:27 +0900 Subject: [PATCH 2/3] Handle no memory case --- Modules/gcmodule.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Modules/gcmodule.c b/Modules/gcmodule.c index 19f65d1bf05a65..3fd5f4cd70e832 100644 --- a/Modules/gcmodule.c +++ b/Modules/gcmodule.c @@ -1390,6 +1390,12 @@ invoke_gc_callback(PyThreadState *tstate, const char *phase, } PyObject *phase_obj = PyUnicode_FromString(phase); + if (phase_obj == NULL) { + Py_XDECREF(info); + PyErr_WriteUnraisable(NULL); + return; + } + PyObject *stack[] = {phase_obj, info}; for (Py_ssize_t i=0; icallbacks); i++) { PyObject *r, *cb = PyList_GET_ITEM(gcstate->callbacks, i); From a0932eca3ce8faed347820f4bf17bdd34b161640 Mon Sep 17 00:00:00 2001 From: Dong-hee Na Date: Mon, 1 May 2023 22:34:45 +0900 Subject: [PATCH 3/3] Update NEWS.d --- .../2023-05-01-14-10-38.gh-issue-104028.dxfh13.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Misc/NEWS.d/next/Core and Builtins/2023-05-01-14-10-38.gh-issue-104028.dxfh13.rst b/Misc/NEWS.d/next/Core and Builtins/2023-05-01-14-10-38.gh-issue-104028.dxfh13.rst index 01377d660ec23d..9c35ea88499dce 100644 --- a/Misc/NEWS.d/next/Core and Builtins/2023-05-01-14-10-38.gh-issue-104028.dxfh13.rst +++ b/Misc/NEWS.d/next/Core and Builtins/2023-05-01-14-10-38.gh-issue-104028.dxfh13.rst @@ -1,2 +1,2 @@ -Reduce objects creation while calling callback function on gc. Patch by -Dong-hee Na. +Reduce object creation while calling callback function from gc. +Patch by Dong-hee Na.