Skip to content

Commit

Permalink
pythongh-119053: Implement the fast path for list.__getitem__
Browse files Browse the repository at this point in the history
  • Loading branch information
corona10 committed May 17, 2024
1 parent 100c7ab commit f2cd85c
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 additions & 3 deletions Objects/listobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -647,6 +647,17 @@ list_contains(PyObject *aa, PyObject *el)
return 0;
}

static inline PyObject *
list_item_try_lock_free(PyListObject *a, Py_ssize_t i)
{
PyObject **ob_item = FT_ATOMIC_LOAD_PTR(a->ob_item);
PyObject *item = FT_ATOMIC_LOAD_PTR(ob_item[i]);
if (!item || !_Py_TryIncrefCompare(&ob_item[i], item)) {
return NULL;
}
return item;
}

static PyObject *
list_item(PyObject *aa, Py_ssize_t i)
{
Expand All @@ -655,15 +666,21 @@ list_item(PyObject *aa, Py_ssize_t i)
PyErr_SetObject(PyExc_IndexError, &_Py_STR(list_err));
return NULL;
}
PyObject *item;
#ifdef Py_GIL_DISABLED
PyObject *item = list_item_try_lock_free(a, i);
if (item != NULL) {
goto end;
}
#endif
Py_BEGIN_CRITICAL_SECTION(a);
item = Py_NewRef(a->ob_item[i]);
Py_END_CRITICAL_SECTION();
#ifdef Py_GIL_DISABLED
end:
if (!_Py_IsOwnedByCurrentThread((PyObject *)a) && !_PyObject_GC_IS_SHARED(a)) {
_PyObject_GC_SET_SHARED(a);
}
#endif
item = Py_NewRef(a->ob_item[i]);
Py_END_CRITICAL_SECTION();
return item;
}

Expand Down

0 comments on commit f2cd85c

Please sign in to comment.