44#include "Python.h"
55#include "pycore_call.h" // _Py_CheckFunctionResult()
66#include "pycore_ceval.h" // _Py_EnterRecursiveCallTstate()
7+ #include "pycore_freelist.h"
78#include "pycore_object.h"
89#include "pycore_pyerrors.h"
910#include "pycore_pystate.h" // _PyThreadState_GET()
@@ -85,9 +86,12 @@ PyCMethod_New(PyMethodDef *ml, PyObject *self, PyObject *module, PyTypeObject *c
8586 "flag but no class" );
8687 return NULL ;
8788 }
88- PyCMethodObject * om = PyObject_GC_New (PyCMethodObject , & PyCMethod_Type );
89+ PyCMethodObject * om = _Py_FREELIST_POP (PyCMethodObject , pycmethodobject );
8990 if (om == NULL ) {
90- return NULL ;
91+ om = PyObject_GC_New (PyCMethodObject , & PyCMethod_Type );
92+ if (om == NULL ) {
93+ return NULL ;
94+ }
9195 }
9296 om -> mm_class = (PyTypeObject * )Py_NewRef (cls );
9397 op = (PyCFunctionObject * )om ;
@@ -98,9 +102,12 @@ PyCMethod_New(PyMethodDef *ml, PyObject *self, PyObject *module, PyTypeObject *c
98102 "but no METH_METHOD flag" );
99103 return NULL ;
100104 }
101- op = PyObject_GC_New (PyCFunctionObject , & PyCFunction_Type );
105+ op = _Py_FREELIST_POP (PyCFunctionObject , pycfunctionobject );
102106 if (op == NULL ) {
103- return NULL ;
107+ op = PyObject_GC_New (PyCFunctionObject , & PyCFunction_Type );
108+ if (op == NULL ) {
109+ return NULL ;
110+ }
104111 }
105112 }
106113
@@ -171,7 +178,14 @@ meth_dealloc(PyObject *self)
171178 Py_XDECREF (PyCFunction_GET_CLASS (m ));
172179 Py_XDECREF (m -> m_self );
173180 Py_XDECREF (m -> m_module );
174- PyObject_GC_Del (m );
181+ if (m -> m_ml -> ml_flags & METH_METHOD ) {
182+ assert (Py_IS_TYPE (self , & PyCMethod_Type ));
183+ _Py_FREELIST_FREE (pycmethodobject , m , PyObject_GC_Del );
184+ }
185+ else {
186+ assert (Py_IS_TYPE (self , & PyCFunction_Type ));
187+ _Py_FREELIST_FREE (pycfunctionobject , m , PyObject_GC_Del );
188+ }
175189 Py_TRASHCAN_END ;
176190}
177191
0 commit comments