Skip to content

Commit 18fb4ea

Browse files
committed
gh-158790: Move the list shift logic into helpers
Move the free-threaded/GIL split out of ins1() and list_ass_item_lock_held() into two small direction-specific list_shift_items_*() helpers, so the call sites stay platform-independent and each #ifdef lives in one place. The helpers are static inline and keep the original loop shapes; the generated code is unchanged in both build configurations.
1 parent 58c3939 commit 18fb4ea

1 file changed

Lines changed: 32 additions & 15 deletions

File tree

‎Objects/listobject.c‎

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -481,6 +481,36 @@ end:;
481481
static void ptr_wise_atomic_memmove(PyListObject *a, PyObject **dest,
482482
PyObject **src, Py_ssize_t n);
483483

484+
static inline void
485+
list_shift_items_right_lock_held(PyListObject *self, Py_ssize_t first,
486+
Py_ssize_t last)
487+
{
488+
#ifdef Py_GIL_DISABLED
489+
ptr_wise_atomic_memmove(self, &self->ob_item[first + 1],
490+
&self->ob_item[first], last - first);
491+
#else
492+
PyObject **items = self->ob_item;
493+
for (Py_ssize_t i = last; --i >= first; ) {
494+
items[i + 1] = items[i];
495+
}
496+
#endif
497+
}
498+
499+
static inline void
500+
list_shift_items_left_lock_held(PyListObject *self, Py_ssize_t first,
501+
Py_ssize_t last)
502+
{
503+
#ifdef Py_GIL_DISABLED
504+
ptr_wise_atomic_memmove(self, &self->ob_item[first],
505+
&self->ob_item[first + 1], last - first);
506+
#else
507+
PyObject **items = self->ob_item;
508+
for (Py_ssize_t i = first; i < last; i++) {
509+
items[i] = items[i + 1];
510+
}
511+
#endif
512+
}
513+
484514
static int
485515
ins1(PyListObject *self, Py_ssize_t where, PyObject *v)
486516
{
@@ -503,15 +533,9 @@ ins1(PyListObject *self, Py_ssize_t where, PyObject *v)
503533
if (where > n)
504534
where = n;
505535
items = self->ob_item;
506-
#ifdef Py_GIL_DISABLED
507536
if (where < n) {
508-
ptr_wise_atomic_memmove(self, &items[where + 1], &items[where],
509-
n - where);
537+
list_shift_items_right_lock_held(self, where, n);
510538
}
511-
#else
512-
for (Py_ssize_t i = n; --i >= where; )
513-
FT_ATOMIC_STORE_PTR_RELEASE(items[i+1], items[i]);
514-
#endif
515539
FT_ATOMIC_STORE_PTR_RELEASE(items[where], Py_NewRef(v));
516540
return 0;
517541
}
@@ -1155,16 +1179,9 @@ list_ass_item_lock_held(PyListObject *a, Py_ssize_t i, PyObject *v)
11551179
PyObject *tmp = a->ob_item[i];
11561180
if (v == NULL) {
11571181
Py_ssize_t size = Py_SIZE(a);
1158-
#ifdef Py_GIL_DISABLED
11591182
if (i < size - 1) {
1160-
ptr_wise_atomic_memmove(a, &a->ob_item[i], &a->ob_item[i + 1],
1161-
size - 1 - i);
1183+
list_shift_items_left_lock_held(a, i, size - 1);
11621184
}
1163-
#else
1164-
for (Py_ssize_t idx = i; idx < size - 1; idx++) {
1165-
FT_ATOMIC_STORE_PTR_RELEASE(a->ob_item[idx], a->ob_item[idx + 1]);
1166-
}
1167-
#endif
11681185
Py_SET_SIZE(a, size - 1);
11691186
}
11701187
else {

0 commit comments

Comments
 (0)