diff options
author | Christian Heimes <christian@cheimes.de> | 2008-02-06 12:44:34 (GMT) |
---|---|---|
committer | Christian Heimes <christian@cheimes.de> | 2008-02-06 12:44:34 (GMT) |
commit | 6075a82243c7646dcdd45b424cf3e5c676f31ccf (patch) | |
tree | d349099fa3b0f7fe9d5c3b364b929ffe86a1db58 /Objects/methodobject.c | |
parent | 45eda6469124855d349666184c6f92058a9e08f0 (diff) | |
download | cpython-6075a82243c7646dcdd45b424cf3e5c676f31ccf.zip cpython-6075a82243c7646dcdd45b424cf3e5c676f31ccf.tar.gz cpython-6075a82243c7646dcdd45b424cf3e5c676f31ccf.tar.bz2 |
Limit free list of method and builtin function objects to 256 entries each.
Diffstat (limited to 'Objects/methodobject.c')
-rw-r--r-- | Objects/methodobject.c | 20 |
1 files changed, 17 insertions, 3 deletions
diff --git a/Objects/methodobject.c b/Objects/methodobject.c index 3daa9dd..17e905b 100644 --- a/Objects/methodobject.c +++ b/Objects/methodobject.c @@ -4,7 +4,12 @@ #include "Python.h" #include "structmember.h" +/* Free list for method objects to safe malloc/free overhead + * The m_self element is used to chain the objects. + */ static PyCFunctionObject *free_list = NULL; +static int numfree = 0; +#define MAXFREELIST 256 PyObject * PyCFunction_NewEx(PyMethodDef *ml, PyObject *self, PyObject *module) @@ -14,6 +19,7 @@ PyCFunction_NewEx(PyMethodDef *ml, PyObject *self, PyObject *module) if (op != NULL) { free_list = (PyCFunctionObject *)(op->m_self); PyObject_INIT(op, &PyCFunction_Type); + numfree--; } else { op = PyObject_GC_New(PyCFunctionObject, &PyCFunction_Type); @@ -125,8 +131,14 @@ meth_dealloc(PyCFunctionObject *m) _PyObject_GC_UNTRACK(m); Py_XDECREF(m->m_self); Py_XDECREF(m->m_module); - m->m_self = (PyObject *)free_list; - free_list = m; + if (numfree < MAXFREELIST) { + m->m_self = (PyObject *)free_list; + free_list = m; + numfree++; + } + else { + PyObject_GC_Del(m); + } } static PyObject * @@ -346,14 +358,16 @@ PyCFunction_Fini(void) PyCFunctionObject *v = free_list; free_list = (PyCFunctionObject *)(v->m_self); PyObject_GC_Del(v); + numfree--; } + assert(numfree == 0); } /* PyCFunction_New() is now just a macro that calls PyCFunction_NewEx(), but it's part of the API so we need to keep a function around that existing C extensions can call. */ - + #undef PyCFunction_New PyAPI_FUNC(PyObject *) PyCFunction_New(PyMethodDef *, PyObject *); |