dict.reserve and other tricks

Terry Reedy tjreedy at udel.edu
Thu Nov 16 22:04:52 EST 2006


<bearophileHUGS at lycos.com> wrote in message 
news:1163724294.126102.216870 at e3g2000cwe.googlegroups.com...
> If you keep adding elements to a CPython dict/set, it periodically
> rebuilds itself. So maybe dict.reserve(n) and a set.reserve(n) methods
> may help, reserving enough (empty) memory for about n *distinct* keys
> the programmer wants to add to the dict/set in a short future. I have
> seen that the the C API of the dicts doesn't allow this, and I don't
> know if this can be implemented modifying the dicts a bit. Do you think
> this may be useful?

Ideas for user-controlled 'performance tuning' have been proposed on the 
development list.  GVR has rejected such as more trouble than they are 
worth.  Instead, effort has gone into making the implementation as good as 
he and others know how.

Since you are writing extensions, you can create a built-in subclass of 
dict to experiment with.  I presume the 2.5 default dict should be a model.

Terry Jan Reedy






More information about the Python-list mailing list