Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize asyncio.Future schedule/add/remove callback #76529

1st1 opened this issue Dec 16, 2017 · 3 comments

Optimize asyncio.Future schedule/add/remove callback #76529

1st1 opened this issue Dec 16, 2017 · 3 comments


Copy link

1st1 commented Dec 16, 2017

BPO 32348
Nosy @asvetlov, @1st1
  • bpo-32348: Optimize asyncio.Future schedule/add/remove callback. #4907
  • Files
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = ''
    closed_at = <Date 2017-12-18.01:22:51.716>
    created_at = <Date 2017-12-16.23:50:13.245>
    labels = ['3.7', 'expert-asyncio']
    title = 'Optimize asyncio.Future schedule/add/remove callback'
    updated_at = <Date 2017-12-18.01:22:51.715>
    user = '' fields:

    activity = <Date 2017-12-18.01:22:51.715>
    actor = 'yselivanov'
    assignee = 'yselivanov'
    closed = True
    closed_date = <Date 2017-12-18.01:22:51.716>
    closer = 'yselivanov'
    components = ['asyncio']
    creation = <Date 2017-12-16.23:50:13.245>
    creator = 'yselivanov'
    dependencies = []
    files = ['47335']
    hgrepos = []
    issue_num = 32348
    keywords = ['patch']
    message_count = 3.0
    messages = ['308481', '308502', '308503']
    nosy_count = 2.0
    nosy_names = ['asvetlov', 'yselivanov']
    pr_nums = ['4907']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = None
    url = ''
    versions = ['Python 3.7']

    Copy link
    Member Author

    1st1 commented Dec 16, 2017

    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.

    @1st1 1st1 added the 3.7 (EOL) end of life label Dec 16, 2017
    @1st1 1st1 self-assigned this Dec 16, 2017
    Copy link
    Member Author

    1st1 commented Dec 18, 2017

    New changeset 1b7c11f by Yury Selivanov in branch 'master':
    bpo-32348: Optimize asyncio.Future schedule/add/remove callback. (bpo-4907)

    Copy link
    Member Author

    1st1 commented Dec 18, 2017

    The attached benchmark from shows that the updated Task is 15% faster.

    @1st1 1st1 closed this as completed Dec 18, 2017
    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    None yet

    No branches or pull requests

    1 participant