diff --git a/Include/cpython/pystate.h b/Include/cpython/pystate.h index a1dbe2b9ee..5c10b2af9f 100644 --- a/Include/cpython/pystate.h +++ b/Include/cpython/pystate.h @@ -180,6 +180,10 @@ struct _ts { PyObject *dict; /* Stores per-thread state */ + /* Using a cache is very effective since typically only a single slice is + created and then deleted again. */ + PySliceObject *slice_cache; + int gilstate_counter; PyObject *async_exc; /* Asynchronous exception to raise */ diff --git a/Include/internal/pycore_interp.h b/Include/internal/pycore_interp.h index 3ff15ca0bf..1a155c7996 100644 --- a/Include/internal/pycore_interp.h +++ b/Include/internal/pycore_interp.h @@ -196,9 +196,6 @@ struct _is { struct _Py_unicode_state unicode; struct _Py_float_state float_state; struct _Py_long_state long_state; - /* Using a cache is very effective since typically only a single slice is - created and then deleted again. */ - PySliceObject *slice_cache; struct _Py_tuple_state tuple; struct _Py_list_state list; diff --git a/Objects/sliceobject.c b/Objects/sliceobject.c index df4d27b589..1afdba0aaa 100644 --- a/Objects/sliceobject.c +++ b/Objects/sliceobject.c @@ -95,11 +95,6 @@ PyObject _Py_EllipsisObject = _PyObject_STRUCT_INIT(&PyEllipsis_Type); void _PySlice_Fini(PyInterpreterState *interp) { - PySliceObject *obj = interp->slice_cache; - if (obj != NULL) { - interp->slice_cache = NULL; - PyObject_GC_Del(obj); - } } /* start, stop, and step are python objects with None indicating no @@ -111,11 +106,11 @@ _PyBuildSlice_Consume2(PyObject *start, PyObject *stop, PyObject *step) { assert(start != NULL && stop != NULL && step != NULL); - PyInterpreterState *interp = _PyInterpreterState_GET(); + PyThreadState *tstate = _PyThreadState_GET(); PySliceObject *obj; - if (interp->slice_cache != NULL) { - obj = interp->slice_cache; - interp->slice_cache = NULL; + if (tstate->slice_cache != NULL) { + obj = tstate->slice_cache; + tstate->slice_cache = NULL; _Py_NewReference((PyObject *)obj); } else { @@ -343,13 +338,13 @@ Create a slice object. This is used for extended slicing (e.g. a[0:10:2])."); static void slice_dealloc(PySliceObject *r) { - PyInterpreterState *interp = _PyInterpreterState_GET(); + PyThreadState *tstate = _PyThreadState_GET(); _PyObject_GC_UNTRACK(r); Py_DECREF(r->step); Py_DECREF(r->start); Py_DECREF(r->stop); - if (interp->slice_cache == NULL) { - interp->slice_cache = r; + if (tstate->slice_cache == NULL) { + tstate->slice_cache = r; } else { PyObject_GC_Del(r); diff --git a/Python/pystate.c b/Python/pystate.c index b0f8d42b17..2b9f5b053b 100644 --- a/Python/pystate.c +++ b/Python/pystate.c @@ -1372,7 +1372,11 @@ PyThreadState_Clear(PyThreadState *tstate) Py_CLEAR(tstate->dict); Py_CLEAR(tstate->async_exc); - + if (tstate->slice_cache) { + PySliceObject *obj = tstate->slice_cache; + tstate->slice_cache = NULL; + PyObject_GC_Del(obj); + } Py_CLEAR(tstate->curexc_type); Py_CLEAR(tstate->curexc_value); Py_CLEAR(tstate->curexc_traceback);