summaryrefslogtreecommitdiff
path: root/Objects/sliceobject.c
diff options
context:
space:
mode:
authorAntoine Pitrou <solipsis@pitrou.net>2011-11-18 20:14:34 +0100
committerAntoine Pitrou <solipsis@pitrou.net>2011-11-18 20:14:34 +0100
commit20c31ce6ca51dcf5219497f8f4bb07110b9144a2 (patch)
tree611551f5c38e1236465dffd4d11fe4fa5ad6d494 /Objects/sliceobject.c
parent8dfd71517db698c6ee73f98a42bfa3fc81ee83db (diff)
downloadcpython-20c31ce6ca51dcf5219497f8f4bb07110b9144a2.tar.gz
Issue #10227: Add an allocation cache for a single slice object.
Patch by Stefan Behnel.
Diffstat (limited to 'Objects/sliceobject.c')
-rw-r--r--Objects/sliceobject.c36
1 files changed, 29 insertions, 7 deletions
diff --git a/Objects/sliceobject.c b/Objects/sliceobject.c
index 2f5c045f36..c4a190755c 100644
--- a/Objects/sliceobject.c
+++ b/Objects/sliceobject.c
@@ -80,19 +80,38 @@ PyObject _Py_EllipsisObject = {
};
-/* Slice object implementation
+/* Slice object implementation */
- start, stop, and step are python objects with None indicating no
+/* Using a cache is very effective since typically only a single slice is
+ * created and then deleted again
+ */
+static PySliceObject *slice_cache = NULL;
+void PySlice_Fini(void)
+{
+ PySliceObject *obj = slice_cache;
+ if (obj != NULL) {
+ slice_cache = NULL;
+ PyObject_Del(obj);
+ }
+}
+
+/* start, stop, and step are python objects with None indicating no
index is present.
*/
PyObject *
PySlice_New(PyObject *start, PyObject *stop, PyObject *step)
{
- PySliceObject *obj = PyObject_New(PySliceObject, &PySlice_Type);
-
- if (obj == NULL)
- return NULL;
+ PySliceObject *obj;
+ if (slice_cache != NULL) {
+ obj = slice_cache;
+ slice_cache = NULL;
+ _Py_NewReference((PyObject *)obj);
+ } else {
+ obj = PyObject_New(PySliceObject, &PySlice_Type);
+ if (obj == NULL)
+ return NULL;
+ }
if (step == NULL) step = Py_None;
Py_INCREF(step);
@@ -260,7 +279,10 @@ slice_dealloc(PySliceObject *r)
Py_DECREF(r->step);
Py_DECREF(r->start);
Py_DECREF(r->stop);
- PyObject_Del(r);
+ if (slice_cache == NULL)
+ slice_cache = r;
+ else
+ PyObject_Del(r);
}
static PyObject *