Index: Python/pythonrun.c =================================================================== --- Python/pythonrun.c (Revision 88298) +++ Python/pythonrun.c (Arbeitskopie) @@ -503,6 +503,7 @@ PyLong_Fini(); PyFloat_Fini(); PyDict_Fini(); + PySlice_Fini(); /* Cleanup Unicode implementation */ _PyUnicode_Fini(); Index: Include/pythonrun.h =================================================================== --- Include/pythonrun.h (Revision 88298) +++ Include/pythonrun.h (Arbeitskopie) @@ -212,6 +212,7 @@ PyAPI_FUNC(void) PyFloat_Fini(void); PyAPI_FUNC(void) PyOS_FiniInterrupts(void); PyAPI_FUNC(void) _PyGC_Fini(void); +PyAPI_FUNC(void) PySlice_Fini(); #endif /* Stuff with no proper home (yet) */ Index: Objects/sliceobject.c =================================================================== --- Objects/sliceobject.c (Revision 88298) +++ Objects/sliceobject.c (Arbeitskopie) @@ -51,20 +51,39 @@ }; -/* 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() +{ + 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); + PySliceObject *obj; + if (slice_cache != NULL) { + obj = slice_cache; + slice_cache = NULL; + PyObject_INIT(obj, &PySlice_Type); + } else { + obj = PyObject_New(PySliceObject, &PySlice_Type); + if (obj == NULL) + return NULL; + } - if (obj == NULL) - return NULL; - if (step == NULL) step = Py_None; Py_INCREF(step); if (start == NULL) start = Py_None; @@ -231,7 +250,10 @@ 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 *