summaryrefslogtreecommitdiffstats
path: root/Modules
diff options
context:
space:
mode:
authorDong-hee Na <donghee.na@python.org>2023-05-01 14:03:24 (GMT)
committerGitHub <noreply@github.com>2023-05-01 14:03:24 (GMT)
commite1476942525ae847875dab55541bef4a8a99dd3d (patch)
tree481a8fab1a3d3f3e2a9f1366f1e33a770efb4269 /Modules
parent4181d078fc945313568eb39965cb9190881606b5 (diff)
downloadcpython-e1476942525ae847875dab55541bef4a8a99dd3d.zip
cpython-e1476942525ae847875dab55541bef4a8a99dd3d.tar.gz
cpython-e1476942525ae847875dab55541bef4a8a99dd3d.tar.bz2
gh-104028: Reduce object creation while calling callback function from gc (gh-104030)
Diffstat (limited to 'Modules')
-rw-r--r--Modules/gcmodule.c12
1 files changed, 11 insertions, 1 deletions
diff --git a/Modules/gcmodule.c b/Modules/gcmodule.c
index 966c1e6..3fd5f4c 100644
--- a/Modules/gcmodule.c
+++ b/Modules/gcmodule.c
@@ -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));
}