diff options
author | Raymond Hettinger <python@rcn.com> | 2004-05-05 05:37:53 (GMT) |
---|---|---|
committer | Raymond Hettinger <python@rcn.com> | 2004-05-05 05:37:53 (GMT) |
commit | 0468e416c16f5d2ac072628b0d31e129e0b2776f (patch) | |
tree | 48514aedd414e7ea6214a5ba8834a396b75d631f /Objects | |
parent | 8aa8c84d18aecdc63279ccd82cf4c7815900fe1c (diff) | |
download | cpython-0468e416c16f5d2ac072628b0d31e129e0b2776f.zip cpython-0468e416c16f5d2ac072628b0d31e129e0b2776f.tar.gz cpython-0468e416c16f5d2ac072628b0d31e129e0b2776f.tar.bz2 |
SF patch #947476: Apply freelist technique to lists
Re-use list object bodies. Saves calls to malloc() and free() for
faster list instantiation and deallocation.
Diffstat (limited to 'Objects')
-rw-r--r-- | Objects/listobject.c | 21 |
1 files changed, 17 insertions, 4 deletions
diff --git a/Objects/listobject.c b/Objects/listobject.c index 7a2cdea..f3aee39 100644 --- a/Objects/listobject.c +++ b/Objects/listobject.c @@ -49,6 +49,11 @@ list_resize(PyListObject *self, int newsize) return 0; } +/* Empty list reuse scheme to save calls to malloc and free */ +#define MAXFREELISTS 80 +static PyListObject *free_lists[MAXFREELISTS]; +static int num_free_lists = 0; + PyObject * PyList_New(int size) { @@ -63,9 +68,14 @@ PyList_New(int size) if (nbytes / sizeof(PyObject *) != (size_t)size) { return PyErr_NoMemory(); } - op = PyObject_GC_New(PyListObject, &PyList_Type); - if (op == NULL) { - return NULL; + if (num_free_lists) { + num_free_lists--; + op = free_lists[num_free_lists]; + _Py_NewReference((PyObject *)op); + } else { + op = PyObject_GC_New(PyListObject, &PyList_Type); + if (op == NULL) + return NULL; } if (size <= 0) { op->ob_item = NULL; @@ -233,7 +243,10 @@ list_dealloc(PyListObject *op) } PyMem_FREE(op->ob_item); } - op->ob_type->tp_free((PyObject *)op); + if (num_free_lists < MAXFREELISTS && PyList_CheckExact(op)) + free_lists[num_free_lists++] = op; + else + op->ob_type->tp_free((PyObject *)op); Py_TRASHCAN_SAFE_END(op) } |