#endif
struct _Py_tuple_state tuple;
struct _Py_float_state float_state;
+
+ /* Using a cache is very effective since typically only a single slice is
+ created and then deleted again. */
+ PySliceObject *slice_cache;
};
/* Used by _PyImport_Cleanup() */
extern void _PySet_Fini(void);
extern void _PyBytes_Fini(void);
extern void _PyFloat_Fini(PyThreadState *tstate);
-extern void _PySlice_Fini(void);
+extern void _PySlice_Fini(PyThreadState *tstate);
extern void _PyAsyncGen_Fini(void);
extern void PyOS_FiniInterrupts(void);
-Tuple free lists, empty tuple singleton, and float free list are no longer
-shared by all interpreters: each interpreter now its own free lists.
+The tuple free lists, the empty tuple singleton, the float free list, and the
+slice cache are no longer shared by all interpreters: each interpreter now has
+its own free lists and caches.
#include "Python.h"
#include "pycore_abstract.h" // _PyIndex_Check()
-#include "pycore_object.h"
+#include "pycore_object.h" // _PyObject_GC_TRACK()
#include "structmember.h" // PyMemberDef
static PyObject *
/* Slice object implementation */
-/* 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)
+void _PySlice_Fini(PyThreadState *tstate)
{
- PySliceObject *obj = slice_cache;
+ PyInterpreterState *interp = tstate->interp;
+ PySliceObject *obj = interp->slice_cache;
if (obj != NULL) {
- slice_cache = NULL;
+ interp->slice_cache = NULL;
PyObject_GC_Del(obj);
}
}
PyObject *
PySlice_New(PyObject *start, PyObject *stop, PyObject *step)
{
+ PyInterpreterState *interp = _PyInterpreterState_GET();
PySliceObject *obj;
- if (slice_cache != NULL) {
- obj = slice_cache;
- slice_cache = NULL;
+ if (interp->slice_cache != NULL) {
+ obj = interp->slice_cache;
+ interp->slice_cache = NULL;
_Py_NewReference((PyObject *)obj);
} else {
obj = PyObject_GC_New(PySliceObject, &PySlice_Type);
static void
slice_dealloc(PySliceObject *r)
{
+ PyInterpreterState *interp = _PyInterpreterState_GET();
_PyObject_GC_UNTRACK(r);
Py_DECREF(r->step);
Py_DECREF(r->start);
Py_DECREF(r->stop);
- if (slice_cache == NULL)
- slice_cache = r;
- else
+ if (interp->slice_cache == NULL) {
+ interp->slice_cache = r;
+ }
+ else {
PyObject_GC_Del(r);
+ }
}
static PyObject *