]> granicus.if.org Git - python/commitdiff
Issue #10227: Add an allocation cache for a single slice object.
authorAntoine Pitrou <solipsis@pitrou.net>
Fri, 18 Nov 2011 19:14:34 +0000 (20:14 +0100)
committerAntoine Pitrou <solipsis@pitrou.net>
Fri, 18 Nov 2011 19:14:34 +0000 (20:14 +0100)
Patch by Stefan Behnel.

Include/pythonrun.h
Misc/NEWS
Objects/sliceobject.c
Python/pythonrun.c

index bdad15c652de629abb5498c56547cfe401c74832..fc6c85434adcda1dd75796b173e1a21790b71adc 100644 (file)
@@ -211,6 +211,7 @@ PyAPI_FUNC(void) PyByteArray_Fini(void);
 PyAPI_FUNC(void) PyFloat_Fini(void);
 PyAPI_FUNC(void) PyOS_FiniInterrupts(void);
 PyAPI_FUNC(void) _PyGC_Fini(void);
+PyAPI_FUNC(void) PySlice_Fini(void);
 
 PyAPI_DATA(PyThreadState *) _Py_Finalizing;
 #endif
index 0a1c67b0188ba6bcf94f602b53801cc46939912b..183e4ed49a5e461e5833b82d3dc176e6e27efd33 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -10,6 +10,9 @@ What's New in Python 3.3 Alpha 1?
 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
   the raw stream instead of limiting itself to the buffer size.
 
index 2f5c045f36e1f37e50b2bf5f31aa5d0ba92c5bdc..c4a190755c3309f39e4cb042af7d137941bdd801 100644 (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.
 */
 
 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 *
index 0f2f0501cde78af2cad81c3333123fed784c3ccb..0c267fc832694b459c7918aea383c56120c53828 100644 (file)
@@ -531,6 +531,7 @@ Py_Finalize(void)
     PyLong_Fini();
     PyFloat_Fini();
     PyDict_Fini();
+    PySlice_Fini();
 
     /* Cleanup Unicode implementation */
     _PyUnicode_Fini();