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: Optimise "in" operations on tuples of consts
Type: Stage:
Components: Interpreter Core Versions: Python 2.5
process
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: rhettinger Nosy List: collinwinter, rhettinger
Priority: normal Keywords: patch

Created on 2006-06-01 20:03 by collinwinter, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
optimise_in_const_ops.patch collinwinter, 2006-06-01 20:03 Optimise "in" ops on constant tuples, against r46597
Messages (2)
msg50407 - (view) Author: Collin Winter (collinwinter) * (Python committer) Date: 2006-06-01 20:03
The following patch adds a peephole optimisation to the
compiler that optimises expressions like

x in (5, 6, 7)

to

x in frozenset([5, 6, 7])

That is, any "in" operation on tuples of constants will
be optimised to "in" operations on frozensets. Note
that the patch does not handle lists, as "in [constant
list]" is already optimised to "in (constant tuple)".

Additional tests for Lib/test/test_peepholer.py are
included. This patch is against r46597.


Some benchmarks (all times in usecs per loop):

./python -mtimeit '5 in (5, 6, 7, 8, 9)'
Unoptimised: 0.376
Optimised:   0.437

./python -mtimeit '9 in (5, 6, 7, 8, 9)'
Unoptimised: 1.14
Optimised:   0.436

./python -mtimeit '89 in (5, 6, 7, 8, 9)'
Unoptimised: 1.32
Optimised:   0.498
msg50408 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2006-06-03 08:22
Logged In: YES 
user_id=80475

Sorry, this enticing idea has already been explored and 
rejected.  This is issue is that the transformation is not 
semanatically neutral.  Currently, writing "{} in (1,2,3)" 
returns False, but after the transformation would raise an 
exception, "TypeError: dict objects are unhashable".
History
Date User Action Args
2022-04-11 14:56:17adminsetgithub: 43447
2006-06-01 20:03:07collinwintercreate