]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-104028: Reduce object creation while calling callback function from gc (gh-104030)
authorDong-hee Na <donghee.na@python.org>
Mon, 1 May 2023 14:03:24 +0000 (23:03 +0900)
committerGitHub <noreply@github.com>
Mon, 1 May 2023 14:03:24 +0000 (14:03 +0000)
Misc/NEWS.d/next/Core and Builtins/2023-05-01-14-10-38.gh-issue-104028.dxfh13.rst [new file with mode: 0644]
Modules/gcmodule.c

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 (file)
index 0000000..9c35ea8
--- /dev/null
@@ -0,0 +1,2 @@
+Reduce object creation while calling callback function from gc.
+Patch by Dong-hee Na.
index 966c1e615502ef80a1b3ac940f72a1335fcce93f..3fd5f4cd70e832357fa7b596fda75c721b34c6cc 100644 (file)
@@ -1388,10 +1388,19 @@ invoke_gc_callback(PyThreadState *tstate, const char *phase,
             return;
         }
     }
+
+    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; 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);
         }
@@ -1400,6 +1409,7 @@ invoke_gc_callback(PyThreadState *tstate, const char *phase,
         }
         Py_DECREF(cb);
     }
+    Py_DECREF(phase_obj);
     Py_XDECREF(info);
     assert(!_PyErr_Occurred(tstate));
 }