Skip to content

Commit 2318bed

Browse files
GH-103092: port _asyncio freelist to module state (#104196)
1 parent 81fc135 commit 2318bed

File tree

1 file changed

+25
-27
lines changed

1 file changed

+25
-27
lines changed

Diff for: Modules/_asynciomodule.c

+25-27
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ module _asyncio
1818
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=8fd17862aa989c69]*/
1919

2020

21+
#define FI_FREELIST_MAXLEN 255
22+
23+
typedef struct futureiterobject futureiterobject;
24+
2125
/* State of the _asyncio module */
2226
typedef struct {
2327
PyTypeObject *FutureIterType;
@@ -67,6 +71,9 @@ typedef struct {
6771

6872
/* Counter for autogenerated Task names */
6973
uint64_t task_name_counter;
74+
75+
futureiterobject *fi_freelist;
76+
Py_ssize_t fi_freelist_len;
7077
} asyncio_state;
7178

7279
static inline asyncio_state *
@@ -1574,28 +1581,24 @@ FutureObj_dealloc(PyObject *self)
15741581

15751582
/*********************** Future Iterator **************************/
15761583

1577-
typedef struct {
1584+
typedef struct futureiterobject {
15781585
PyObject_HEAD
15791586
FutureObj *future;
15801587
} futureiterobject;
15811588

15821589

1583-
#define FI_FREELIST_MAXLEN 255
1584-
static futureiterobject *fi_freelist = NULL;
1585-
static Py_ssize_t fi_freelist_len = 0;
1586-
1587-
15881590
static void
15891591
FutureIter_dealloc(futureiterobject *it)
15901592
{
15911593
PyTypeObject *tp = Py_TYPE(it);
1594+
asyncio_state *state = get_asyncio_state_by_def((PyObject *)it);
15921595
PyObject_GC_UnTrack(it);
15931596
tp->tp_clear((PyObject *)it);
15941597

1595-
if (fi_freelist_len < FI_FREELIST_MAXLEN) {
1596-
fi_freelist_len++;
1597-
it->future = (FutureObj*) fi_freelist;
1598-
fi_freelist = it;
1598+
if (state->fi_freelist_len < FI_FREELIST_MAXLEN) {
1599+
state->fi_freelist_len++;
1600+
it->future = (FutureObj*) state->fi_freelist;
1601+
state->fi_freelist = it;
15991602
}
16001603
else {
16011604
PyObject_GC_Del(it);
@@ -1799,17 +1802,12 @@ future_new_iter(PyObject *fut)
17991802
futureiterobject *it;
18001803

18011804
asyncio_state *state = get_asyncio_state_by_def((PyObject *)fut);
1802-
if (!Future_Check(state, fut)) {
1803-
PyErr_BadInternalCall();
1804-
return NULL;
1805-
}
1806-
18071805
ENSURE_FUTURE_ALIVE(state, fut)
18081806

1809-
if (fi_freelist_len) {
1810-
fi_freelist_len--;
1811-
it = fi_freelist;
1812-
fi_freelist = (futureiterobject*) it->future;
1807+
if (state->fi_freelist_len) {
1808+
state->fi_freelist_len--;
1809+
it = state->fi_freelist;
1810+
state->fi_freelist = (futureiterobject*) it->future;
18131811
it->future = NULL;
18141812
_Py_NewReference((PyObject*) it);
18151813
}
@@ -3556,22 +3554,22 @@ _asyncio_current_task_impl(PyObject *module, PyObject *loop)
35563554

35573555

35583556
static void
3559-
module_free_freelists(void)
3557+
module_free_freelists(asyncio_state *state)
35603558
{
35613559
PyObject *next;
35623560
PyObject *current;
35633561

3564-
next = (PyObject*) fi_freelist;
3562+
next = (PyObject*) state->fi_freelist;
35653563
while (next != NULL) {
3566-
assert(fi_freelist_len > 0);
3567-
fi_freelist_len--;
3564+
assert(state->fi_freelist_len > 0);
3565+
state->fi_freelist_len--;
35683566

35693567
current = next;
35703568
next = (PyObject*) ((futureiterobject*) current)->future;
35713569
PyObject_GC_Del(current);
35723570
}
3573-
assert(fi_freelist_len == 0);
3574-
fi_freelist = NULL;
3571+
assert(state->fi_freelist_len == 0);
3572+
state->fi_freelist = NULL;
35753573
}
35763574

35773575
static int
@@ -3603,7 +3601,7 @@ module_traverse(PyObject *mod, visitproc visit, void *arg)
36033601
Py_VISIT(state->context_kwname);
36043602

36053603
// Visit freelist.
3606-
PyObject *next = (PyObject*) fi_freelist;
3604+
PyObject *next = (PyObject*) state->fi_freelist;
36073605
while (next != NULL) {
36083606
PyObject *current = next;
36093607
Py_VISIT(current);
@@ -3640,7 +3638,7 @@ module_clear(PyObject *mod)
36403638

36413639
Py_CLEAR(state->context_kwname);
36423640

3643-
module_free_freelists();
3641+
module_free_freelists(state);
36443642

36453643
return 0;
36463644
}

0 commit comments

Comments
 (0)