# define Py_futureiters_MAXFREELIST 255
# define Py_object_stack_chunks_MAXFREELIST 4
# define Py_unicode_writers_MAXFREELIST 1
+# define Py_pymethodobjects_MAXFREELIST 20
// A generic freelist of either PyObjects or other data structures.
struct _Py_freelist {
struct _Py_freelist futureiters;
struct _Py_freelist object_stack_chunks;
struct _Py_freelist unicode_writers;
+ struct _Py_freelist pymethodobjects;
};
#ifdef __cplusplus
#include "Python.h"
#include "pycore_call.h" // _PyObject_VectorcallTstate()
#include "pycore_ceval.h" // _PyEval_GetBuiltin()
+#include "pycore_freelist.h"
#include "pycore_object.h"
#include "pycore_pyerrors.h"
#include "pycore_pystate.h" // _PyThreadState_GET()
PyErr_BadInternalCall();
return NULL;
}
- PyMethodObject *im = PyObject_GC_New(PyMethodObject, &PyMethod_Type);
+ PyMethodObject *im = _Py_FREELIST_POP(PyMethodObject, pymethodobjects);
if (im == NULL) {
- return NULL;
+ im = PyObject_GC_New(PyMethodObject, &PyMethod_Type);
+ if (im == NULL) {
+ return NULL;
+ }
}
im->im_weakreflist = NULL;
im->im_func = Py_NewRef(func);
PyObject_ClearWeakRefs((PyObject *)im);
Py_DECREF(im->im_func);
Py_XDECREF(im->im_self);
- PyObject_GC_Del(im);
+ assert(Py_IS_TYPE(self, &PyMethod_Type));
+ _Py_FREELIST_FREE(pymethodobjects, (PyObject *)im, PyObject_GC_Del);
}
static PyObject *
}
clear_freelist(&freelists->unicode_writers, is_finalization, PyMem_Free);
clear_freelist(&freelists->ints, is_finalization, free_object);
+ clear_freelist(&freelists->pymethodobjects, is_finalization, free_object);
}
/*