[Python-Dev] Prospective Peephole Transformation

M.-A. Lemburg mal at egenix.com
Sat Feb 19 13:06:37 CET 2005

Raymond Hettinger wrote:
>>Hmm, what if you'd teach tuples to do faster contains lookups for
>>string or integer only content, e.g. by introducing sub-types for
>>string-only and integer-only tuples ?!
> For a linear search, tuples are already pretty darned good and leave
> room for only microscopic O(n) improvements.  The bigger win comes from
> using a better algorithm and data structure -- hashing beats linear
> search hands-down.  The constant search time is faster for all n>1,
> resulting in much improved scalability.  No tweaking of
> tuple.__contains__() can match it.
> Sets are the right data structure for fast membership testing.  I would
> love for sets to be used internally while letting users continue to
> write the clean looking code shown above.

That's what I was thinking off: if the compiler can detect
the constant nature and the use of a common type, it could
set a flag in the tuple type telling it about this feature.
The tuple could then convert the tuple contents to a set
internally and when the __contains__ hook is first called
and use the set for the lookup.

Alternatively, you could use a sub-type for a few common

In either case you would have to teach marshal how to
treat the extra bit of information.

The user won't notice all this in the Python program
and can continue to write clean code (in some cases,
even cleaner code than before - I usually use the keyword
hack to force certain things into the locals at module
load time, but would love to get rid off this).

Marc-Andre Lemburg

Professional Python Services directly from the Source  (#1, Feb 19 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