diff options
author | Eddie Elizondo <eduardo.elizondorueda@gmail.com> | 2023-04-22 19:39:37 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-04-22 19:39:37 (GMT) |
commit | ea2c0016502472aa8baa3149050ada776d17a009 (patch) | |
tree | e9e3935a9b71a1f88ac18c24fe512d199880ff90 /Objects/sliceobject.c | |
parent | 916de04fd1838530096336aadb3b94b774ed6c90 (diff) | |
download | cpython-ea2c0016502472aa8baa3149050ada776d17a009.zip cpython-ea2c0016502472aa8baa3149050ada776d17a009.tar.gz cpython-ea2c0016502472aa8baa3149050ada776d17a009.tar.bz2 |
gh-84436: Implement Immortal Objects (gh-19474)
This is the implementation of PEP683
Motivation:
The PR introduces the ability to immortalize instances in CPython which bypasses reference counting. Tagging objects as immortal allows up to skip certain operations when we know that the object will be around for the entire execution of the runtime.
Note that this by itself will bring a performance regression to the runtime due to the extra reference count checks. However, this brings the ability of having truly immutable objects that are useful in other contexts such as immutable data sharing between sub-interpreters.
Diffstat (limited to 'Objects/sliceobject.c')
-rw-r--r-- | Objects/sliceobject.c | 15 |
1 files changed, 13 insertions, 2 deletions
diff --git a/Objects/sliceobject.c b/Objects/sliceobject.c index 584ebce..e6776ac 100644 --- a/Objects/sliceobject.c +++ b/Objects/sliceobject.c @@ -29,6 +29,16 @@ ellipsis_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) return Py_NewRef(Py_Ellipsis); } +static void +ellipsis_dealloc(PyObject *ellipsis) +{ + /* This should never get called, but we also don't want to SEGV if + * we accidentally decref Ellipsis out of existence. Instead, + * since Ellipsis is an immortal object, re-set the reference count. + */ + _Py_SetImmortal(ellipsis); +} + static PyObject * ellipsis_repr(PyObject *op) { @@ -51,7 +61,7 @@ PyTypeObject PyEllipsis_Type = { "ellipsis", /* tp_name */ 0, /* tp_basicsize */ 0, /* tp_itemsize */ - 0, /*never called*/ /* tp_dealloc */ + ellipsis_dealloc, /* tp_dealloc */ 0, /* tp_vectorcall_offset */ 0, /* tp_getattr */ 0, /* tp_setattr */ @@ -89,7 +99,8 @@ PyTypeObject PyEllipsis_Type = { PyObject _Py_EllipsisObject = { _PyObject_EXTRA_INIT - 1, &PyEllipsis_Type + { _Py_IMMORTAL_REFCNT }, + &PyEllipsis_Type }; |