[Python-Dev] Prospective Peephole Transformation
M.-A. Lemburg
mal at egenix.com
Fri Feb 18 21:57:16 CET 2005
Raymond Hettinger wrote:
> Based on some ideas from Skip, I had tried transforming the likes of "x
> in (1,2,3)" into "x in frozenset([1,2,3])". When applicable, it
> substantially simplified the generated code and converted the O(n)
> lookup into an O(1) step. There were substantial savings even if the
> set contained only a single entry. When disassembled, the bytecode is
> not only much shorter, it is also much more readable (corresponding
> almost directly to the original source).
>
> The problem with the transformation was that it didn't handle the case
> where x was non-hashable and it would raise a TypeError instead of
> returning False as it should. That situation arose once in the email
> module's test suite.
>
> To get it to work, I would have to introduce a frozenset subtype:
>
> class Searchset(frozenset):
> def __contains__(self, element):
> try:
> return frozenset.__contains__(self, element)
> except TypeError:
> return False
>
> Then, the transformation would be "x in Searchset([1, 2, 3])". Since
> the new Searchset object goes in the constant table, marshal would have
> to be taught how to save and restore the object.
>
> This is a more complicated than the original frozenset version of the
> patch, so I would like to get feedback on whether you guys think it is
> worth it.
Wouldn't it help a lot more if the compiler would detect that
(1,2,3) is immutable and convert it into a constant at
compile time ?!
The next step would then be to have Python roll out these
loops (in -O mode).
--
Marc-Andre Lemburg
eGenix.com
Professional Python Services directly from the Source (#1, Feb 18 2005)
>>> Python/Zope Consulting and Support ... http://www.egenix.com/
>>> mxODBC.Zope.Database.Adapter ... http://zope.egenix.com/
>>> mxODBC, mxDateTime, mxTextTools ... http://python.egenix.com/
________________________________________________________________________
::: Try mxODBC.Zope.DA for Windows,Linux,Solaris,FreeBSD for free ! ::::
More information about the Python-Dev
mailing list