Skip to content

Commit

Permalink
slice: move slice_cache to per-thread state
Browse files Browse the repository at this point in the history
  • Loading branch information
colesbury committed Apr 23, 2023
1 parent 45bdd27 commit 07f5f8c
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 16 deletions.
4 changes: 4 additions & 0 deletions Include/cpython/pystate.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
3 changes: 0 additions & 3 deletions Include/internal/pycore_interp.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
19 changes: 7 additions & 12 deletions Objects/sliceobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand Down Expand Up @@ -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);
Expand Down
6 changes: 5 additions & 1 deletion Python/pystate.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 07f5f8c

Please sign in to comment.