classification
Title: AST optimizer: Change a list into tuple in iterations and containment tests
Type: performance Stage: resolved
Components: Interpreter Core Versions: Python 3.8
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: benjamin.peterson, brett.cannon, ncoghlan, scoder, serhiy.storchaka, yselivanov
Priority: normal Keywords: patch

Created on 2018-02-23 21:44 by serhiy.storchaka, last changed 2018-03-11 08:57 by serhiy.storchaka. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 5842 merged serhiy.storchaka, 2018-02-23 22:23
Messages (4)
msg312670 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2018-02-23 21:44
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
msg312703 - (view) Author: Stefan Behnel (scoder) * (Python committer) Date: 2018-02-24 08:22
An obvious optimisation, if you ask me. PR looks good to me superficially, but I don't know the AST code well.
msg312828 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2018-02-25 17:52
The direct inspiration of this optimization was your note that similar optimization was implemented in Python.
msg313588 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2018-03-11 08:54
New changeset 3f7e9aa2ef215917b9f1521441f67f4ecd33a1bc by Serhiy Storchaka in branch 'master':
bpo-32925: Optimized iterating and containing test for literal lists (GH-5842)
https://github.com/python/cpython/commit/3f7e9aa2ef215917b9f1521441f67f4ecd33a1bc
History
Date User Action Args
2018-03-11 08:57:09serhiy.storchakasetstatus: open -> closed
resolution: fixed
stage: patch review -> resolved
2018-03-11 08:54:49serhiy.storchakasetmessages: + msg313588
2018-02-25 17:52:02serhiy.storchakasetmessages: + msg312828
2018-02-24 08:22:46scodersetnosy: + scoder
messages: + msg312703
2018-02-23 22:23:08serhiy.storchakasetkeywords: + patch
stage: patch review
pull_requests: + pull_request5617
2018-02-23 21:44:18serhiy.storchakacreate