# define Py_futureiters_MAXFREELIST 255
# define Py_object_stack_chunks_MAXFREELIST 4
# define Py_unicode_writers_MAXFREELIST 1
+# define Py_pycfunctionobject_MAXFREELIST 16
+# define Py_pycmethodobject_MAXFREELIST 16
# define Py_pymethodobjects_MAXFREELIST 20
// A generic freelist of either PyObjects or other data structures.
struct _Py_freelist futureiters;
struct _Py_freelist object_stack_chunks;
struct _Py_freelist unicode_writers;
+ struct _Py_freelist pycfunctionobject;
+ struct _Py_freelist pycmethodobject;
struct _Py_freelist pymethodobjects;
};
#include "Python.h"
#include "pycore_call.h" // _Py_CheckFunctionResult()
#include "pycore_ceval.h" // _Py_EnterRecursiveCallTstate()
+#include "pycore_freelist.h"
#include "pycore_object.h"
#include "pycore_pyerrors.h"
#include "pycore_pystate.h" // _PyThreadState_GET()
"flag but no class");
return NULL;
}
- PyCMethodObject *om = PyObject_GC_New(PyCMethodObject, &PyCMethod_Type);
+ PyCMethodObject *om = _Py_FREELIST_POP(PyCMethodObject, pycmethodobject);
if (om == NULL) {
- return NULL;
+ om = PyObject_GC_New(PyCMethodObject, &PyCMethod_Type);
+ if (om == NULL) {
+ return NULL;
+ }
}
om->mm_class = (PyTypeObject*)Py_NewRef(cls);
op = (PyCFunctionObject *)om;
"but no METH_METHOD flag");
return NULL;
}
- op = PyObject_GC_New(PyCFunctionObject, &PyCFunction_Type);
+ op = _Py_FREELIST_POP(PyCFunctionObject, pycfunctionobject);
if (op == NULL) {
- return NULL;
+ op = PyObject_GC_New(PyCFunctionObject, &PyCFunction_Type);
+ if (op == NULL) {
+ return NULL;
+ }
}
}
Py_XDECREF(PyCFunction_GET_CLASS(m));
Py_XDECREF(m->m_self);
Py_XDECREF(m->m_module);
- PyObject_GC_Del(m);
+ if (m->m_ml->ml_flags & METH_METHOD) {
+ assert(Py_IS_TYPE(self, &PyCMethod_Type));
+ _Py_FREELIST_FREE(pycmethodobject, m, PyObject_GC_Del);
+ }
+ else {
+ assert(Py_IS_TYPE(self, &PyCFunction_Type));
+ _Py_FREELIST_FREE(pycfunctionobject, m, PyObject_GC_Del);
+ }
Py_TRASHCAN_END;
}
}
clear_freelist(&freelists->unicode_writers, is_finalization, PyMem_Free);
clear_freelist(&freelists->ints, is_finalization, free_object);
+ clear_freelist(&freelists->pycfunctionobject, is_finalization, PyObject_GC_Del);
+ clear_freelist(&freelists->pycmethodobject, is_finalization, PyObject_GC_Del);
clear_freelist(&freelists->pymethodobjects, is_finalization, free_object);
}