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.

classification
Title: Optimize literal comparisons and contains
Type: performance Stage: resolved
Components: Interpreter Core Versions: Python 3.11
process
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: Nosy List: BTaskaya, Crowthebird, pablogsal, rhettinger, serhiy.storchaka
Priority: normal Keywords: patch

Created on 2021-11-26 23:59 by Crowthebird, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 29810 closed Crowthebird, 2021-11-27 00:05
Messages (9)
msg407114 - (view) Author: Jeremiah Gabriel Pascual (Crowthebird) * Date: 2021-11-26 23:59
Most operations with literals are optimized as well, so why shouldn't the comparison/contain operators be? I created a new bug report for it because of this fact and the TODO in the `fold_compare` function in `Python/ast_opt.c`.
msg407124 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2021-11-27 02:06
This PR looks to be the same as one that was recently rejected.

Compare:
   https://github.com/python/cpython/pull/29639/files
   https://github.com/python/cpython/pull/29810/files

See discussion at:
   https://bugs.python.org/issue45843
msg407126 - (view) Author: Jeremiah Gabriel Pascual (Crowthebird) * Date: 2021-11-27 02:39
It doesn't seem to make sense why other operations on literals are optimized but these particular ones aren't optimized (much).
msg407127 - (view) Author: Jeremiah Gabriel Pascual (Crowthebird) * Date: 2021-11-27 02:41
If this bug report isn't accepted, for how long would the TODO remain in `Python/ast_opt.c`?
msg407138 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2021-11-27 10:38
> It doesn't seem to make sense why other operations on literals are optimized but these particular ones aren't optimized (much).

The existing optimizer optimizes the following cases (note that the parser does not produce negative or complex numbers, they are created by the optimizer):

   -1
   1-2j
   1/3
   16*1024
   2**32-1
   1<<12
   b'a'[0]

They all are extremely common. Virtually every Python file contain some of such expressions, and they are often used in loops. In contrary, it is difficult to find any example of using comparison operations with all constant operands.
msg407144 - (view) Author: Jeremiah Gabriel Pascual (Crowthebird) * Date: 2021-11-27 13:34
List and sets as right operands in literal contains are optimized to constant tuples and frozensets, and I'd like to take this optimization a step further.
msg407145 - (view) Author: Batuhan Taskaya (BTaskaya) * (Python committer) Date: 2021-11-27 13:36
We just rejected the same issue 2 days ago. If you feel very strong about this; instead of creating new ticket in the same place, you might want to try python-dev instead. 

Re: the todo comment, feel free to send a patch that removes it. I don't thank that is still applicable.
msg407157 - (view) Author: Pablo Galindo Salgado (pablogsal) * (Python committer) Date: 2021-11-27 17:05
I agree with Serhiy and Batuhan. Please reach to python-dev of you really want to pursue this even after what has been already discussed.
msg407158 - (view) Author: Pablo Galindo Salgado (pablogsal) * (Python committer) Date: 2021-11-27 17:06
On the other hand, we can probably just remove the TODO
History
Date User Action Args
2022-04-11 14:59:52adminsetgithub: 90065
2021-11-27 17:06:32pablogsalsetmessages: + msg407158
2021-11-27 17:05:47pablogsalsetstatus: open -> closed
resolution: rejected
messages: + msg407157

stage: patch review -> resolved
2021-11-27 13:36:15BTaskayasetmessages: + msg407145
2021-11-27 13:34:03Crowthebirdsetmessages: + msg407144
2021-11-27 10:38:53serhiy.storchakasetmessages: + msg407138
2021-11-27 02:41:16Crowthebirdsetmessages: + msg407127
2021-11-27 02:39:41Crowthebirdsetmessages: + msg407126
2021-11-27 02:06:44rhettingersetnosy: + serhiy.storchaka, pablogsal, BTaskaya, rhettinger
messages: + msg407124
2021-11-27 00:05:33Crowthebirdsetkeywords: + patch
stage: patch review
pull_requests: + pull_request28042
2021-11-26 23:59:07Crowthebirdcreate