Skip to content

Commit 07f5f8c

Browse files
committed
slice: move slice_cache to per-thread state
1 parent 45bdd27 commit 07f5f8c

File tree

4 files changed

+16
-16
lines changed

4 files changed

+16
-16
lines changed

Include/cpython/pystate.h

+4
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,10 @@ struct _ts {
180180

181181
PyObject *dict; /* Stores per-thread state */
182182

183+
/* Using a cache is very effective since typically only a single slice is
184+
created and then deleted again. */
185+
PySliceObject *slice_cache;
186+
183187
int gilstate_counter;
184188

185189
PyObject *async_exc; /* Asynchronous exception to raise */

Include/internal/pycore_interp.h

-3
Original file line numberDiff line numberDiff line change
@@ -196,9 +196,6 @@ struct _is {
196196
struct _Py_unicode_state unicode;
197197
struct _Py_float_state float_state;
198198
struct _Py_long_state long_state;
199-
/* Using a cache is very effective since typically only a single slice is
200-
created and then deleted again. */
201-
PySliceObject *slice_cache;
202199

203200
struct _Py_tuple_state tuple;
204201
struct _Py_list_state list;

Objects/sliceobject.c

+7-12
Original file line numberDiff line numberDiff line change
@@ -95,11 +95,6 @@ PyObject _Py_EllipsisObject = _PyObject_STRUCT_INIT(&PyEllipsis_Type);
9595

9696
void _PySlice_Fini(PyInterpreterState *interp)
9797
{
98-
PySliceObject *obj = interp->slice_cache;
99-
if (obj != NULL) {
100-
interp->slice_cache = NULL;
101-
PyObject_GC_Del(obj);
102-
}
10398
}
10499

105100
/* start, stop, and step are python objects with None indicating no
@@ -111,11 +106,11 @@ _PyBuildSlice_Consume2(PyObject *start, PyObject *stop, PyObject *step)
111106
{
112107
assert(start != NULL && stop != NULL && step != NULL);
113108

114-
PyInterpreterState *interp = _PyInterpreterState_GET();
109+
PyThreadState *tstate = _PyThreadState_GET();
115110
PySliceObject *obj;
116-
if (interp->slice_cache != NULL) {
117-
obj = interp->slice_cache;
118-
interp->slice_cache = NULL;
111+
if (tstate->slice_cache != NULL) {
112+
obj = tstate->slice_cache;
113+
tstate->slice_cache = NULL;
119114
_Py_NewReference((PyObject *)obj);
120115
}
121116
else {
@@ -343,13 +338,13 @@ Create a slice object. This is used for extended slicing (e.g. a[0:10:2]).");
343338
static void
344339
slice_dealloc(PySliceObject *r)
345340
{
346-
PyInterpreterState *interp = _PyInterpreterState_GET();
341+
PyThreadState *tstate = _PyThreadState_GET();
347342
_PyObject_GC_UNTRACK(r);
348343
Py_DECREF(r->step);
349344
Py_DECREF(r->start);
350345
Py_DECREF(r->stop);
351-
if (interp->slice_cache == NULL) {
352-
interp->slice_cache = r;
346+
if (tstate->slice_cache == NULL) {
347+
tstate->slice_cache = r;
353348
}
354349
else {
355350
PyObject_GC_Del(r);

Python/pystate.c

+5-1
Original file line numberDiff line numberDiff line change
@@ -1372,7 +1372,11 @@ PyThreadState_Clear(PyThreadState *tstate)
13721372

13731373
Py_CLEAR(tstate->dict);
13741374
Py_CLEAR(tstate->async_exc);
1375-
1375+
if (tstate->slice_cache) {
1376+
PySliceObject *obj = tstate->slice_cache;
1377+
tstate->slice_cache = NULL;
1378+
PyObject_GC_Del(obj);
1379+
}
13761380
Py_CLEAR(tstate->curexc_type);
13771381
Py_CLEAR(tstate->curexc_value);
13781382
Py_CLEAR(tstate->curexc_traceback);

0 commit comments

Comments
 (0)