8000 gh-119053: Implement the fast path for list.__getitem__ · python/cpython@f2cd85c · GitHub
[go: up one dir, main page]

Skip to content

Commit f2cd85c

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

File tree

1 file changed

+20
-3
lines changed

1 file changed

+20
-3
lines changed

Objects/listobject.c

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -647,6 +647,17 @@ list_contains(PyObject *aa, PyObject *el)
647647
return 0;
648648
}
649649

650+
static inline PyObject *
651+
list_item_try_lock_free(PyListObject *a, Py_ssize_t i)
652+
{
653+
PyObject **ob_item = FT_ATOMIC_LOAD_PTR(a->ob_item);
654+
PyObject *item = FT_ATOMIC_LOAD_PTR(ob_item[i]);
655+
if (!item || !_Py_TryIncrefCompare(&ob_item[i], item)) {
656+
return NULL;
657+
}
658+
return item;
659+
}
660+
650661
static PyObject *
651662
list_item(PyObject *aa, Py_ssize_t i)
652663
{
@@ -655,15 +666,21 @@ list_item(PyObject *aa, Py_ssize_t i)
655666
PyErr_SetObject(PyExc_IndexError, &_Py_STR(list_err));
656667
return NULL;
657668
}
658-
PyObject *item;
669+
#ifdef Py_GIL_DISABLED
670+
PyObject *item = list_item_try_lock_free(a, i);
671+
if (item != NULL) {
672+
goto end;
673+
}
674+
#endif
659675
Py_BEGIN_CRITICAL_SECTION(a);
676+
item = Py_NewRef(a->ob_item[i]);
677+
Py_END_CRITICAL_SECTION();
660678
#ifdef Py_GIL_DISABLED
679+
end:
661680
if (!_Py_IsOwnedByCurrentThread((PyObject *)a) && !_PyObject_GC_IS_SHARED(a)) {
662681
_PyObject_GC_SET_SHARED(a);
663682
}
664683
#endif
665-
item = Py_NewRef(a->ob_item[i]);
666-
Py_END_CRITICAL_SECTION();
667684
return item;
668685
}
669686

0 commit comments

Comments
 (0)
0