Skip to content

Commit e147694

Browse files
authored
gh-104028: Reduce object creation while calling callback function from gc (gh-104030)
1 parent 4181d07 commit e147694

File tree

2 files changed

+13
-1
lines changed

2 files changed

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

Modules/gcmodule.c

+11-1
Original file line numberDiff line numberDiff line change
@@ -1388,10 +1388,19 @@ invoke_gc_callback(PyThreadState *tstate, const char *phase,
13881388
return;
13891389
}
13901390
}
1391+
1392+
PyObject *phase_obj = PyUnicode_FromString(phase);
1393+
if (phase_obj == NULL) {
1394+
Py_XDECREF(info);
1395+
PyErr_WriteUnraisable(NULL);
1396+
return;
1397+
}
1398+
1399+
PyObject *stack[] = {phase_obj, info};
13911400
for (Py_ssize_t i=0; i<PyList_GET_SIZE(gcstate->callbacks); i++) {
13921401
PyObject *r, *cb = PyList_GET_ITEM(gcstate->callbacks, i);
13931402
Py_INCREF(cb); /* make sure cb doesn't go away */
1394-
r = PyObject_CallFunction(cb, "sO", phase, info);
1403+
r = PyObject_Vectorcall(cb, stack, 2, NULL);
13951404
if (r == NULL) {
13961405
PyErr_WriteUnraisable(cb);
13971406
}
@@ -1400,6 +1409,7 @@ invoke_gc_callback(PyThreadState *tstate, const char *phase,
14001409
}
14011410
Py_DECREF(cb);
14021411
}
1412+
Py_DECREF(phase_obj);
14031413
Py_XDECREF(info);
14041414
assert(!_PyErr_Occurred(tstate));
14051415
}

0 commit comments

Comments
 (0)