[Patches] [ python-Patches-1499095 ] Optimise "in" operations on tuples of consts

SourceForge.net noreply at sourceforge.net
Sat Jun 3 10:22:08 CEST 2006


Patches item #1499095, was opened at 2006-06-01 15:03
Message generated for change (Comment added) made by rhettinger
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=305470&aid=1499095&group_id=5470

Please note that this message will contain a full copy of the comment thread,
including the initial issue submission, for this request,
not just the latest update.
Category: Parser/Compiler
Group: Python 2.5
>Status: Closed
>Resolution: Rejected
Priority: 5
Submitted By: Collin Winter (collinwinter)
Assigned to: Raymond Hettinger (rhettinger)
Summary: Optimise "in" operations on tuples of consts

Initial Comment:
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

----------------------------------------------------------------------

>Comment By: Raymond Hettinger (rhettinger)
Date: 2006-06-03 03:22

Message:
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".

----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=305470&aid=1499095&group_id=5470


More information about the Patches mailing list