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

AST optimizer: Change a list into tuple in iterations and containment tests #77106

Closed
serhiy-storchaka opened this issue Feb 23, 2018 · 4 comments
Labels
3.8 (EOL) end of life interpreter-core (Objects, Python, Grammar, and Parser dirs) performance Performance or resource usage

Comments

@serhiy-storchaka
Copy link
Member

BPO 32925
Nosy @brettcannon, @ncoghlan, @scoder, @benjaminp, @serhiy-storchaka, @1st1
PRs
  • bpo-32925: Optimized iterating and containing test for literal lists #5842
  • 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 = None
    closed_at = <Date 2018-03-11.08:57:09.458>
    created_at = <Date 2018-02-23.21:44:18.191>
    labels = ['interpreter-core', '3.8', 'performance']
    title = 'AST optimizer: Change a list into tuple in iterations and containment tests'
    updated_at = <Date 2018-03-11.08:57:09.457>
    user = 'https://github.com/serhiy-storchaka'

    bugs.python.org fields:

    activity = <Date 2018-03-11.08:57:09.457>
    actor = 'serhiy.storchaka'
    assignee = 'none'
    closed = True
    closed_date = <Date 2018-03-11.08:57:09.458>
    closer = 'serhiy.storchaka'
    components = ['Interpreter Core']
    creation = <Date 2018-02-23.21:44:18.191>
    creator = 'serhiy.storchaka'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 32925
    keywords = ['patch']
    message_count = 4.0
    messages = ['312670', '312703', '312828', '313588']
    nosy_count = 6.0
    nosy_names = ['brett.cannon', 'ncoghlan', 'scoder', 'benjamin.peterson', 'serhiy.storchaka', 'yselivanov']
    pr_nums = ['5842']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'performance'
    url = 'https://bugs.python.org/issue32925'
    versions = ['Python 3.8']

    @serhiy-storchaka
    Copy link
    Member Author

    Currently a list of constants is replaced with constant tuple in x in [1, 2] and for x in [1, 2]. The resulted AST is the same as for x in (1, 2) and for x in (1, 2).

    The proposed simple PR extends this optimization to lists containing non-constants. x in [a, b] will be changed into x in (a, b) and for x in [a, b] will be changed into for x in (a, b). Since creating a tuple is faster than creating a list the latter form is a tiny bit faster.

    $ ./python -m timeit -s 'a, b = 1, 2' -- 'for x in [a, b]: pass'
    5000000 loops, best of 5: 93.6 nsec per loop
    
    $ ./python -m timeit -s 'a, b = 1, 2' -- 'for x in (a, b): pass'
    5000000 loops, best of 5: 74.3 nsec per loop

    ./python -m timeit -s 'a, b = 1, 2' -- '1 in [a, b]'
    5000000 loops, best of 5: 58.9 nsec per loop

    $ ./python -m timeit -s 'a, b = 1, 2' -- '1 in (a, b)'
    10000000 loops, best of 5: 39.3 nsec per loop

    @serhiy-storchaka serhiy-storchaka added 3.8 (EOL) end of life interpreter-core (Objects, Python, Grammar, and Parser dirs) performance Performance or resource usage labels Feb 23, 2018
    @scoder
    Copy link
    Contributor

    scoder commented Feb 24, 2018

    An obvious optimisation, if you ask me. PR looks good to me superficially, but I don't know the AST code well.

    @serhiy-storchaka
    Copy link
    Member Author

    The direct inspiration of this optimization was your note that similar optimization was implemented in Python.

    @serhiy-storchaka
    Copy link
    Member Author

    New changeset 3f7e9aa by Serhiy Storchaka in branch 'master':
    bpo-32925: Optimized iterating and containing test for literal lists (GH-5842)
    3f7e9aa

    @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
    Labels
    3.8 (EOL) end of life interpreter-core (Objects, Python, Grammar, and Parser dirs) performance Performance or resource usage
    Projects
    None yet
    Development

    No branches or pull requests

    2 participants