From 5fb201de38d38b04f068aec369f60f0ce1e5de6d Mon Sep 17 00:00:00 2001 From: Donghee Na Date: Fri, 17 May 2024 01:43:34 -0400 Subject: [PATCH] gh-119053: Implement the fast path for list.__getitem__ --- Objects/listobject.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/Objects/listobject.c b/Objects/listobject.c index 7070165014f1379..d2f3b2ad352cdb7 100644 --- a/Objects/listobject.c +++ b/Objects/listobject.c @@ -651,11 +651,18 @@ static PyObject * list_item(PyObject *aa, Py_ssize_t i) { PyListObject *a = (PyListObject *)aa; + PyObject *item; if (!valid_index(i, PyList_GET_SIZE(a))) { PyErr_SetObject(PyExc_IndexError, &_Py_STR(list_err)); return NULL; } - PyObject *item; +#ifdef Py_GIL_DISABLED + PyObject **ob_item = _Py_atomic_load_ptr(&a->ob_item); + item = _Py_atomic_load_ptr(&ob_item[i]); + if (item && _Py_TryIncrefCompare(&ob_item[i], item)) { + return item; + } +#endif Py_BEGIN_CRITICAL_SECTION(a); #ifdef Py_GIL_DISABLED if (!_Py_IsOwnedByCurrentThread((PyObject *)a) && !_PyObject_GC_IS_SHARED(a)) {