Skip to content

Commit efafae7

Browse files
corona10estyxx
authored andcommitted
pythongh-119053: Implement the fast path for list.__getitem__ (pythongh-119112)
1 parent c620799 commit efafae7

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

Objects/listobject.c

+10-5
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,11 @@ list_item_impl(PyListObject *self, Py_ssize_t idx)
351351
if (!valid_index(idx, size)) {
352352
goto exit;
353353
}
354+
#ifdef Py_GIL_DISABLED
355+
item = _Py_NewRefWithLock(self->ob_item[idx]);
356+
#else
354357
item = Py_NewRef(self->ob_item[idx]);
358+
#endif
355359
exit:
356360
Py_END_CRITICAL_SECTION();
357361
return item;
@@ -656,14 +660,15 @@ list_item(PyObject *aa, Py_ssize_t i)
656660
return NULL;
657661
}
658662
PyObject *item;
659-
Py_BEGIN_CRITICAL_SECTION(a);
660663
#ifdef Py_GIL_DISABLED
661-
if (!_Py_IsOwnedByCurrentThread((PyObject *)a) && !_PyObject_GC_IS_SHARED(a)) {
662-
_PyObject_GC_SET_SHARED(a);
664+
item = list_get_item_ref(a, i);
665+
if (item == NULL) {
666+
PyErr_SetObject(PyExc_IndexError, &_Py_STR(list_err));
667+
return NULL;
663668
}
664-
#endif
669+
#else
665670
item = Py_NewRef(a->ob_item[i]);
666-
Py_END_CRITICAL_SECTION();
671+
#endif
667672
return item;
668673
}
669674

0 commit comments

Comments
 (0)