Skip to content

Commit 5fb201d

Browse files
committed
gh-119053: Implement the fast path for list.__getitem__
1 parent 100c7ab commit 5fb201d

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

Objects/listobject.c

+8-1
Original file line numberDiff line numberDiff line change
@@ -651,11 +651,18 @@ static PyObject *
651651
list_item(PyObject *aa, Py_ssize_t i)
652652
{
653653
PyListObject *a = (PyListObject *)aa;
654+
PyObject *item;
654655
if (!valid_index(i, PyList_GET_SIZE(a))) {
655656
PyErr_SetObject(PyExc_IndexError, &_Py_STR(list_err));
656657
return NULL;
657658
}
658-
PyObject *item;
659+
#ifdef Py_GIL_DISABLED
660+
PyObject **ob_item = _Py_atomic_load_ptr(&a->ob_item);
661+
item = _Py_atomic_load_ptr(&ob_item[i]);
662+
if (item && _Py_TryIncrefCompare(&ob_item[i], item)) {
663+
return item;
664+
}
665+
#endif
659666
Py_BEGIN_CRITICAL_SECTION(a);
660667
#ifdef Py_GIL_DISABLED
661668
if (!_Py_IsOwnedByCurrentThread((PyObject *)a) && !_PyObject_GC_IS_SHARED(a)) {

0 commit comments

Comments
 (0)