Skip to content

Commit 47534f3

Browse files
committed
gh-104028: Reduce objects creation while calling callback function on gc
1 parent 93107aa commit 47534f3

File tree

2 files changed

+7
-1
lines changed

2 files changed

+7
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Reduce objects creation while calling callback function on gc. Patch by
2+
Dong-hee Na.

Modules/gcmodule.c

+5-1
Original file line numberDiff line numberDiff line change
@@ -1388,10 +1388,13 @@ invoke_gc_callback(PyThreadState *tstate, const char *phase,
13881388
return;
13891389
}
13901390
}
1391+
1392+
PyObject *phase_obj = PyUnicode_FromString(phase);
1393+
PyObject *stack[] = {phase_obj, info};
13911394
for (Py_ssize_t i=0; i<PyList_GET_SIZE(gcstate->callbacks); i++) {
13921395
PyObject *r, *cb = PyList_GET_ITEM(gcstate->callbacks, i);
13931396
Py_INCREF(cb); /* make sure cb doesn't go away */
1394-
r = PyObject_CallFunction(cb, "sO", phase, info);
1397+
r = PyObject_Vectorcall(cb, stack, 2, NULL);
13951398
if (r == NULL) {
13961399
PyErr_WriteUnraisable(cb);
13971400
}
@@ -1400,6 +1403,7 @@ invoke_gc_callback(PyThreadState *tstate, const char *phase,
14001403
}
14011404
Py_DECREF(cb);
14021405
}
1406+
Py_DECREF(phase_obj);
14031407
Py_XDECREF(info);
14041408
assert(!_PyErr_Occurred(tstate));
14051409
}

0 commit comments

Comments
 (0)