@@ -143,8 +143,9 @@ typedef struct {
143143 inherit from native asyncio.Task */
144144 PyObject * non_asyncio_tasks ;
145145
146- /* Set containing all eagerly executing tasks. */
147- PyObject * eager_tasks ;
146+ /* Set containing all 3rd party eagerly executing tasks which don't
147+ inherit from native asyncio.Task */
148+ PyObject * non_asyncio_eager_tasks ;
148149
149150 /* An isinstance type cache for the 'is_coroutine()' function. */
150151 PyObject * iscoroutine_typecache ;
@@ -2180,12 +2181,6 @@ register_task(TaskObj *task)
21802181 llist_insert_tail (head , & task -> task_node );
21812182}
21822183
2183- static int
2184- register_eager_task (asyncio_state * state , PyObject * task )
2185- {
2186- return PySet_Add (state -> eager_tasks , task );
2187- }
2188-
21892184static inline void
21902185unregister_task_safe (TaskObj * task )
21912186{
@@ -2219,12 +2214,6 @@ unregister_task(TaskObj *task)
22192214#endif
22202215}
22212216
2222- static int
2223- unregister_eager_task (asyncio_state * state , PyObject * task )
2224- {
2225- return PySet_Discard (state -> eager_tasks , task );
2226- }
2227-
22282217static int
22292218enter_task (PyObject * loop , PyObject * task )
22302219{
@@ -3473,10 +3462,7 @@ task_eager_start(asyncio_state *state, TaskObj *task)
34733462 return -1 ;
34743463 }
34753464
3476- if (register_eager_task (state , (PyObject * )task ) == -1 ) {
3477- Py_DECREF (prevtask );
3478- return -1 ;
3479- }
3465+ register_task (task );
34803466
34813467 if (PyContext_Enter (task -> task_context ) == -1 ) {
34823468 Py_DECREF (prevtask );
@@ -3506,17 +3492,11 @@ task_eager_start(asyncio_state *state, TaskObj *task)
35063492 Py_DECREF (curtask );
35073493 }
35083494
3509- if (unregister_eager_task (state , (PyObject * )task ) == -1 ) {
3510- retval = -1 ;
3511- }
3512-
35133495 if (PyContext_Exit (task -> task_context ) == -1 ) {
35143496 retval = -1 ;
35153497 }
35163498
3517- if (task -> task_state == STATE_PENDING ) {
3518- register_task (task );
3519- } else {
3499+ if (task -> task_state != STATE_PENDING ) {
35203500 // This seems to really help performance on pyperformance benchmarks
35213501 clear_task_coro (task );
35223502 }
@@ -3735,9 +3715,18 @@ _asyncio__register_eager_task_impl(PyObject *module, PyObject *task)
37353715/*[clinic end generated code: output=dfe1d45367c73f1a input=237f684683398c51]*/
37363716{
37373717 asyncio_state * state = get_asyncio_state (module );
3738- if (register_eager_task (state , task ) < 0 ) {
3718+
3719+ if (Task_Check (state , task )) {
3720+ // task is an asyncio.Task instance or subclass, use efficient
3721+ // linked-list implementation.
3722+ register_task ((TaskObj * )task );
3723+ Py_RETURN_NONE ;
3724+ }
3725+
3726+ if (PySet_Add (state -> non_asyncio_eager_tasks , task ) < 0 ) {
37393727 return NULL ;
37403728 }
3729+
37413730 Py_RETURN_NONE ;
37423731}
37433732
@@ -3785,9 +3774,17 @@ _asyncio__unregister_eager_task_impl(PyObject *module, PyObject *task)
37853774/*[clinic end generated code: output=a426922bd07f23d1 input=9d07401ef14ee048]*/
37863775{
37873776 asyncio_state * state = get_asyncio_state (module );
3788- if (unregister_eager_task (state , task ) < 0 ) {
3777+ if (Task_Check (state , task )) {
3778+ // task is an asyncio.Task instance or subclass, use efficient
3779+ // linked-list implementation.
3780+ unregister_task ((TaskObj * )task );
3781+ Py_RETURN_NONE ;
3782+ }
3783+
3784+ if (PySet_Discard (state -> non_asyncio_eager_tasks , task ) < 0 ) {
37893785 return NULL ;
37903786 }
3787+
37913788 Py_RETURN_NONE ;
37923789}
37933790
@@ -4041,7 +4038,7 @@ _asyncio_all_tasks_impl(PyObject *module, PyObject *loop)
40414038 Py_DECREF (loop );
40424039 return NULL ;
40434040 }
4044- if (PyList_Extend (tasks , state -> eager_tasks ) < 0 ) {
4041+ if (PyList_Extend (tasks , state -> non_asyncio_eager_tasks ) < 0 ) {
40454042 Py_DECREF (tasks );
40464043 Py_DECREF (loop );
40474044 return NULL ;
@@ -4179,7 +4176,7 @@ module_traverse(PyObject *mod, visitproc visit, void *arg)
41794176 Py_VISIT (state -> asyncio_CancelledError );
41804177
41814178 Py_VISIT (state -> non_asyncio_tasks );
4182- Py_VISIT (state -> eager_tasks );
4179+ Py_VISIT (state -> non_asyncio_eager_tasks );
41834180 Py_VISIT (state -> iscoroutine_typecache );
41844181
41854182 Py_VISIT (state -> context_kwname );
@@ -4209,7 +4206,7 @@ module_clear(PyObject *mod)
42094206 Py_CLEAR (state -> asyncio_CancelledError );
42104207
42114208 Py_CLEAR (state -> non_asyncio_tasks );
4212- Py_CLEAR (state -> eager_tasks );
4209+ Py_CLEAR (state -> non_asyncio_eager_tasks );
42134210 Py_CLEAR (state -> iscoroutine_typecache );
42144211
42154212 Py_CLEAR (state -> context_kwname );
@@ -4292,8 +4289,8 @@ module_init(asyncio_state *state)
42924289 goto fail ;
42934290 }
42944291
4295- state -> eager_tasks = PySet_New (NULL );
4296- if (state -> eager_tasks == NULL ) {
4292+ state -> non_asyncio_eager_tasks = PySet_New (NULL );
4293+ if (state -> non_asyncio_eager_tasks == NULL ) {
42974294 goto fail ;
42984295 }
42994296
@@ -4363,14 +4360,6 @@ module_exec(PyObject *mod)
43634360 return -1 ;
43644361 }
43654362
4366- if (PyModule_AddObjectRef (mod , "_scheduled_tasks" , state -> non_asyncio_tasks ) < 0 ) {
4367- return -1 ;
4368- }
4369-
4370- if (PyModule_AddObjectRef (mod , "_eager_tasks" , state -> eager_tasks ) < 0 ) {
4371- return -1 ;
4372- }
4373-
43744363 return 0 ;
43754364}
43764365
0 commit comments