Issue #10227: Add an allocation cache for a single slice object.

Patch by Stefan Behnel.
This commit is contained in:
Antoine Pitrou 2011-11-18 20:14:34 +01:00
parent 2251a3d233
commit f34a0cdc6c
4 changed files with 34 additions and 7 deletions

View file

@ -211,6 +211,7 @@ PyAPI_FUNC(void) PyByteArray_Fini(void);
PyAPI_FUNC(void) PyFloat_Fini(void); PyAPI_FUNC(void) PyFloat_Fini(void);
PyAPI_FUNC(void) PyOS_FiniInterrupts(void); PyAPI_FUNC(void) PyOS_FiniInterrupts(void);
PyAPI_FUNC(void) _PyGC_Fini(void); PyAPI_FUNC(void) _PyGC_Fini(void);
PyAPI_FUNC(void) PySlice_Fini(void);
PyAPI_DATA(PyThreadState *) _Py_Finalizing; PyAPI_DATA(PyThreadState *) _Py_Finalizing;
#endif #endif

View file

@ -10,6 +10,9 @@ What's New in Python 3.3 Alpha 1?
Core and Builtins Core and Builtins
----------------- -----------------
- Issue #10227: Add an allocation cache for a single slice object. Patch by
Stefan Behnel.
- Issue #13393: BufferedReader.read1() now asks the full requested size to - Issue #13393: BufferedReader.read1() now asks the full requested size to
the raw stream instead of limiting itself to the buffer size. the raw stream instead of limiting itself to the buffer size.

View file

@ -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. index is present.
*/ */
PyObject * PyObject *
PySlice_New(PyObject *start, PyObject *stop, PyObject *step) 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;
_Py_NewReference((PyObject *)obj);
} else {
obj = PyObject_New(PySliceObject, &PySlice_Type);
if (obj == NULL) if (obj == NULL)
return NULL; return NULL;
}
if (step == NULL) step = Py_None; if (step == NULL) step = Py_None;
Py_INCREF(step); Py_INCREF(step);
@ -260,6 +279,9 @@ slice_dealloc(PySliceObject *r)
Py_DECREF(r->step); Py_DECREF(r->step);
Py_DECREF(r->start); Py_DECREF(r->start);
Py_DECREF(r->stop); Py_DECREF(r->stop);
if (slice_cache == NULL)
slice_cache = r;
else
PyObject_Del(r); PyObject_Del(r);
} }

View file

@ -531,6 +531,7 @@ Py_Finalize(void)
PyLong_Fini(); PyLong_Fini();
PyFloat_Fini(); PyFloat_Fini();
PyDict_Fini(); PyDict_Fini();
PySlice_Fini();
/* Cleanup Unicode implementation */ /* Cleanup Unicode implementation */
_PyUnicode_Fini(); _PyUnicode_Fini();