Skip to content

Commit 128b502

Browse files
encukouvstinner
andauthored
gh-158660: Simplify _list_extend_impl with macros (GH-163079)
Co-authored-by: Victor Stinner <vstinner@python.org>
1 parent ee1f8ee commit 128b502

1 file changed

Lines changed: 82 additions & 105 deletions

File tree

‎Objects/listobject.c‎

Lines changed: 82 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -1581,145 +1581,122 @@ static inline Py_ALWAYS_INLINE int
15811581
_list_extend_impl(PyListObject *self, PyObject *iterable,
15821582
const int builder_api)
15831583
{
1584-
const int lock_self = !builder_api;
15851584
const int can_shrink = !builder_api;
15861585

1586+
/* Local versions of CRITICAL_SECTION macros, with an `if (lock_self)` to
1587+
* skip locking *self*, but still lock the other input if necessary.
1588+
*/
1589+
#ifdef Py_GIL_DISABLED
1590+
const int lock_self = !builder_api;
1591+
#define BEGIN_CS() \
1592+
{ \
1593+
PyCriticalSection _cs; \
1594+
if (lock_self) { \
1595+
PyCriticalSection_Begin(&_cs, (PyObject*)self); \
1596+
}
1597+
#define END_CS() \
1598+
if (lock_self) { \
1599+
PyCriticalSection_End(&_cs); \
1600+
} \
1601+
}
1602+
#define BEGIN_CS2(O) \
1603+
{ \
1604+
PyCriticalSection _cs; \
1605+
PyCriticalSection2 _cs2; \
1606+
if (lock_self) { \
1607+
PyCriticalSection2_Begin(&_cs2, (PyObject*)self, (PyObject*)(O)); \
1608+
} \
1609+
else { \
1610+
PyCriticalSection_Begin(&_cs, (PyObject*)(O)); \
1611+
}
1612+
#define END_CS2() \
1613+
if (lock_self) { \
1614+
PyCriticalSection2_End(&_cs2); \
1615+
(void)_cs; /* unused */ \
1616+
} \
1617+
else { \
1618+
PyCriticalSection_End(&_cs); \
1619+
(void)_cs2; /* unused */ \
1620+
} \
1621+
}
1622+
#else
1623+
#define BEGIN_CS() {
1624+
#define END_CS() }
1625+
#define BEGIN_CS2(O) {
1626+
#define END_CS2() }
1627+
#endif
1628+
15871629
// Special case:
15881630
// lists and tuples which can use PySequence_Fast ops
15891631
int res = -1;
15901632
if ((PyObject *)self == iterable) {
1591-
if (lock_self) {
1592-
Py_BEGIN_CRITICAL_SECTION(self);
1593-
res = list_inplace_repeat_lock_held(self, 2, can_shrink);
1594-
Py_END_CRITICAL_SECTION();
1595-
}
1596-
else {
1597-
res = list_inplace_repeat_lock_held(self, 2, can_shrink);
1598-
}
1633+
BEGIN_CS();
1634+
res = list_inplace_repeat_lock_held(self, 2, can_shrink);
1635+
END_CS();
15991636
}
16001637
else if (PyList_CheckExact(iterable)) {
1601-
if (lock_self) {
1602-
Py_BEGIN_CRITICAL_SECTION2(self, iterable);
1603-
res = list_extend_lock_held(self, iterable, can_shrink);
1604-
Py_END_CRITICAL_SECTION2();
1605-
}
1606-
else {
1607-
Py_BEGIN_CRITICAL_SECTION(iterable);
1608-
res = list_extend_lock_held(self, iterable, can_shrink);
1609-
Py_END_CRITICAL_SECTION();
1610-
}
1638+
BEGIN_CS2(iterable);
1639+
res = list_extend_lock_held(self, iterable, can_shrink);
1640+
END_CS2();
16111641
}
16121642
else if (PyTuple_CheckExact(iterable)) {
16131643
// Similar to PyList_CheckExact() above, but don't lock iterable
1614-
if (lock_self) {
1615-
Py_BEGIN_CRITICAL_SECTION(self);
1616-
res = list_extend_lock_held(self, iterable, can_shrink);
1617-
Py_END_CRITICAL_SECTION();
1618-
}
1619-
else {
1620-
res = list_extend_lock_held(self, iterable, can_shrink);
1621-
}
1644+
BEGIN_CS();
1645+
res = list_extend_lock_held(self, iterable, can_shrink);
1646+
END_CS();
16221647
}
16231648
else if (PySet_CheckExact(iterable)) {
1624-
if (lock_self) {
1625-
Py_BEGIN_CRITICAL_SECTION2(self, iterable);
1626-
res = list_extend_set(self, (PySetObject *)iterable, can_shrink);
1627-
Py_END_CRITICAL_SECTION2();
1628-
}
1629-
else {
1630-
Py_BEGIN_CRITICAL_SECTION(iterable);
1631-
res = list_extend_set(self, (PySetObject *)iterable, can_shrink);
1632-
Py_END_CRITICAL_SECTION();
1633-
}
1649+
BEGIN_CS2(iterable);
1650+
res = list_extend_set(self, (PySetObject *)iterable, can_shrink);
1651+
END_CS2();
16341652
}
16351653
else if (PyFrozenSet_CheckExact(iterable)) {
16361654
// Similar to PySet_CheckExact() above, but don't lock iterable
1637-
if (lock_self) {
1638-
Py_BEGIN_CRITICAL_SECTION(self);
1639-
res = list_extend_set(self, (PySetObject *)iterable, can_shrink);
1640-
Py_END_CRITICAL_SECTION();
1641-
}
1642-
else {
1643-
res = list_extend_set(self, (PySetObject *)iterable, can_shrink);
1644-
}
1655+
BEGIN_CS();
1656+
res = list_extend_set(self, (PySetObject *)iterable, can_shrink);
1657+
END_CS();
16451658
}
16461659
else if (PyDict_CheckExact(iterable)) {
1647-
if (lock_self) {
1648-
Py_BEGIN_CRITICAL_SECTION2(self, iterable);
1649-
res = list_extend_dict(self, (PyDictObject *)iterable,
1650-
0 /*keys*/, can_shrink);
1651-
Py_END_CRITICAL_SECTION2();
1652-
}
1653-
else {
1654-
Py_BEGIN_CRITICAL_SECTION(iterable);
1655-
res = list_extend_dict(self, (PyDictObject *)iterable,
1656-
0 /*keys*/, can_shrink);
1657-
Py_END_CRITICAL_SECTION();
1658-
}
1660+
BEGIN_CS2(iterable);
1661+
res = list_extend_dict(self, (PyDictObject *)iterable,
1662+
0 /*keys*/, can_shrink);
1663+
END_CS2();
16591664
}
16601665
else if (PyFrozenDict_CheckExact(iterable)) {
16611666
// Similar to PyDict_CheckExact() above, but don't lock iterable
1662-
if (lock_self) {
1663-
Py_BEGIN_CRITICAL_SECTION(self);
1664-
res = list_extend_dict(self, (PyDictObject *)iterable,
1665-
0 /*keys*/, can_shrink);
1666-
Py_END_CRITICAL_SECTION();
1667-
}
1668-
else {
1669-
res = list_extend_dict(self, (PyDictObject *)iterable,
1670-
0 /*keys*/, can_shrink);
1671-
}
1667+
BEGIN_CS();
1668+
res = list_extend_dict(self, (PyDictObject *)iterable,
1669+
0 /*keys*/, can_shrink);
1670+
END_CS();
16721671
}
16731672
else if (Py_IS_TYPE(iterable, &PyDictKeys_Type)) {
16741673
PyDictObject *dict = ((_PyDictViewObject *)iterable)->dv_dict;
1675-
if (lock_self) {
1676-
Py_BEGIN_CRITICAL_SECTION2(self, dict);
1677-
res = list_extend_dict(self, dict, 0 /*keys*/, can_shrink);
1678-
Py_END_CRITICAL_SECTION2();
1679-
}
1680-
else {
1681-
Py_BEGIN_CRITICAL_SECTION(dict);
1682-
res = list_extend_dict(self, dict, 0 /*keys*/, can_shrink);
1683-
Py_END_CRITICAL_SECTION();
1684-
}
1674+
BEGIN_CS2(dict);
1675+
res = list_extend_dict(self, dict, 0 /*keys*/, can_shrink);
1676+
END_CS2();
16851677
}
16861678
else if (Py_IS_TYPE(iterable, &PyDictValues_Type)) {
16871679
PyDictObject *dict = ((_PyDictViewObject *)iterable)->dv_dict;
1688-
if (lock_self) {
1689-
Py_BEGIN_CRITICAL_SECTION2(self, dict);
1690-
res = list_extend_dict(self, dict, 1 /*values*/, can_shrink);
1691-
Py_END_CRITICAL_SECTION2();
1692-
}
1693-
else {
1694-
Py_BEGIN_CRITICAL_SECTION(dict);
1695-
res = list_extend_dict(self, dict, 1 /*values*/, can_shrink);
1696-
Py_END_CRITICAL_SECTION();
1697-
}
1680+
BEGIN_CS2(dict);
1681+
res = list_extend_dict(self, dict, 1 /*values*/, can_shrink);
1682+
END_CS2();
16981683
}
16991684
else if (Py_IS_TYPE(iterable, &PyDictItems_Type)) {
17001685
PyDictObject *dict = ((_PyDictViewObject *)iterable)->dv_dict;
1701-
if (lock_self) {
1702-
Py_BEGIN_CRITICAL_SECTION2(self, dict);
1703-
res = list_extend_dictitems(self, dict, can_shrink);
1704-
Py_END_CRITICAL_SECTION2();
1705-
}
1706-
else {
1707-
Py_BEGIN_CRITICAL_SECTION(dict);
1708-
res = list_extend_dictitems(self, dict, can_shrink);
1709-
Py_END_CRITICAL_SECTION();
1710-
}
1686+
BEGIN_CS2(dict);
1687+
res = list_extend_dictitems(self, dict, can_shrink);
1688+
END_CS2();
17111689
}
17121690
else {
1713-
if (lock_self) {
1714-
Py_BEGIN_CRITICAL_SECTION(self);
1715-
res = list_extend_iter_lock_held(self, iterable, can_shrink);
1716-
Py_END_CRITICAL_SECTION();
1717-
}
1718-
else {
1719-
res = list_extend_iter_lock_held(self, iterable, can_shrink);
1720-
}
1691+
BEGIN_CS();
1692+
res = list_extend_iter_lock_held(self, iterable, can_shrink);
1693+
END_CS();
17211694
}
17221695
return res;
1696+
#undef BEGIN_CS
1697+
#undef END_CS
1698+
#undef BEGIN_CS2
1699+
#undef END_CS2
17231700
}
17241701

17251702
static int

0 commit comments

Comments
 (0)