aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVictor Stinner <vstinner@python.org>2020-06-05 01:14:40 +0200
committerGitHub <noreply@github.com>2020-06-05 01:14:40 +0200
commit7daba6f221e713f7f60c613b246459b07d179f91 (patch)
tree54d09c4a586483468666fc635dee8152e5d8e102 /Objects/sliceobject.c
parentbpo-40521: Make float free list per-interpreter (GH-20636) (diff)
downloadcpython-7daba6f221e713f7f60c613b246459b07d179f91.tar.gz
cpython-7daba6f221e713f7f60c613b246459b07d179f91.tar.bz2
cpython-7daba6f221e713f7f60c613b246459b07d179f91.zip
bpo-40521: Make slice cache per-interpreter (GH-20637)
Each interpreter now has its own slice cache: * Move slice cache into PyInterpreterState. * Add tstate parameter to _PySlice_Fini().
Diffstat (limited to 'Objects/sliceobject.c')
-rw-r--r--Objects/sliceobject.c29
1 files changed, 15 insertions, 14 deletions
diff --git a/Objects/sliceobject.c b/Objects/sliceobject.c
index 391711f711a..f97a570a787 100644
--- a/Objects/sliceobject.c
+++ b/Objects/sliceobject.c
@@ -15,7 +15,7 @@ this type and there is exactly one in existence.
#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 *
@@ -95,16 +95,13 @@ PyObject _Py_EllipsisObject = {
/* 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);
}
}
@@ -116,10 +113,11 @@ void _PySlice_Fini(void)
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);
@@ -324,14 +322,17 @@ 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();
_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 *