[Python-ideas] Keep free list of popular iterator objects

Kyle Fisher anthonyfk at gmail.com
Sun Sep 15 05:12:37 CEST 2013

We tend to do a lot of iterating over dictionaries in our product in some
performance critical areas.  It occurred to me that allocating a new
iterator object every single time seems a little wasteful, especially
considering that there's probably only a handful of them alive at any
time.  Doing a quick test with dictiterobject and 3 free lists (one for
Keys, Values and Items) showed about a 4% speedup in this (best) case:

python -m timeit -s "a = {'k%d' % i: i for i in xrange($2)}" "[_ for _ in

However, this seems like almost too simple of an idea.  Has this been tried
before?  Would it be too little gain?  Given that the extra memory used is
negligible (3 of each iterator?), how much of a performance gain would be
needed to justify it?

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20130914/f3801910/attachment.html>

More information about the Python-ideas mailing list