This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: Optimize asyncio.Future schedule/add/remove callback
Type: Stage: resolved
Components: asyncio Versions: Python 3.7
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: yselivanov Nosy List: asvetlov, yselivanov
Priority: normal Keywords: patch

Created on 2017-12-16 23:50 by yselivanov, last changed 2022-04-11 14:58 by admin. This issue is now closed.

File name Uploaded Description Edit yselivanov, 2017-12-18 01:22
Pull Requests
URL Status Linked Edit
PR 4907 merged yselivanov, 2017-12-16 23:52
Messages (3)
msg308481 - (view) Author: Yury Selivanov (yselivanov) * (Python committer) Date: 2017-12-16 23:50
Key observation: 99.9% of the time Futures and Tasks have only one callback.  

Currently we have a list of callbacks in each Future/Task.  We can avoid list object allocation if we add a field to store the first callback.  This way we'll only need to ever allocate the list if a Future/Task has more than one done callbacks.

asyncio with the proposed patch applied shows that 3-6% better performance of an asyncio echo server implemented using asyncio.streams.  This benchmark involves quite a bit of other Python code, so this improvement is actually quite significant.

The patch consists of:

1. first callback / callbacks list refactoring.

2. a free list implementation for Future.__await__ objects (which are created on almost all awaits in asyncio).

3. a big cleanup of the code, ensuring that Future/Task are always initialized properly.
msg308502 - (view) Author: Yury Selivanov (yselivanov) * (Python committer) Date: 2017-12-18 01:19
New changeset 1b7c11ff0ee3efafbf5b38c3c6f37de5d63efb81 by Yury Selivanov in branch 'master':
bpo-32348: Optimize asyncio.Future schedule/add/remove callback. (#4907)
msg308503 - (view) Author: Yury Selivanov (yselivanov) * (Python committer) Date: 2017-12-18 01:22
The attached benchmark from shows that the updated Task is 15% faster.
Date User Action Args
2022-04-11 14:58:55adminsetgithub: 76529
2017-12-18 01:22:51yselivanovsetstatus: open -> closed
resolution: fixed
stage: patch review -> resolved
2017-12-18 01:22:36yselivanovsetfiles: +

messages: + msg308503
2017-12-18 01:19:50yselivanovsetmessages: + msg308502
2017-12-16 23:52:37yselivanovsetkeywords: + patch
stage: patch review
pull_requests: + pull_request4801
2017-12-16 23:50:13yselivanovcreate