summaryrefslogtreecommitdiff
path: root/Objects/classobject.c
diff options
context:
space:
mode:
Diffstat (limited to 'Objects/classobject.c')
-rw-r--r--Objects/classobject.c23
1 files changed, 18 insertions, 5 deletions
diff --git a/Objects/classobject.c b/Objects/classobject.c
index 9f364e2e88..a9e8c6a9ea 100644
--- a/Objects/classobject.c
+++ b/Objects/classobject.c
@@ -4,10 +4,16 @@
#include "Python.h"
#include "structmember.h"
+/* Free list for method objects to safe malloc/free overhead
+ * The im_self element is used to chain the elements.
+ */
+static PyMethodObject *free_list;
+static int numfree = 0;
+#define MAXFREELIST 256
+
#define TP_DESCR_GET(t) \
(PyType_HasFeature(t, Py_TPFLAGS_HAVE_CLASS) ? (t)->tp_descr_get : NULL)
-
/* Forward */
static PyObject *class_lookup(PyClassObject *, PyObject *,
PyClassObject **);
@@ -2193,8 +2199,6 @@ PyTypeObject PyInstance_Type = {
In case (b), im_self is NULL
*/
-static PyMethodObject *free_list;
-
PyObject *
PyMethod_New(PyObject *func, PyObject *self, PyObject *klass)
{
@@ -2207,6 +2211,7 @@ PyMethod_New(PyObject *func, PyObject *self, PyObject *klass)
if (im != NULL) {
free_list = (PyMethodObject *)(im->im_self);
PyObject_INIT(im, &PyMethod_Type);
+ numfree--;
}
else {
im = PyObject_GC_New(PyMethodObject, &PyMethod_Type);
@@ -2332,8 +2337,14 @@ instancemethod_dealloc(register PyMethodObject *im)
Py_DECREF(im->im_func);
Py_XDECREF(im->im_self);
Py_XDECREF(im->im_class);
- im->im_self = (PyObject *)free_list;
- free_list = im;
+ if (numfree < MAXFREELIST) {
+ im->im_self = (PyObject *)free_list;
+ free_list = im;
+ numfree++;
+ }
+ else {
+ PyObject_GC_Del(im);
+ }
}
static int
@@ -2620,5 +2631,7 @@ PyMethod_Fini(void)
PyMethodObject *im = free_list;
free_list = (PyMethodObject *)(im->im_self);
PyObject_GC_Del(im);
+ numfree--;
}
+ assert(numfree == 0);
}