[issue17338] Add length_hint parameter to list, dict, set constructors to allow efficient presizing

Gregory P. Smith report at bugs.python.org
Tue Mar 5 23:27:59 CET 2013


Gregory P. Smith added the comment:

The gain will be more noticeable the faster the Python implementation it is running under is. It is going to avoid logN relloc's in just about all implementations.  That CPython is relatively slow is not a justification to avoid adding the feature.

I like Christian's proposal of not having it be a constructor argument but having it be a __preallocate__ method on the object.  That keeps it out of the eyes of most users reading the docs (keeping the main interface clear) and lets us put it in a special section of the python docs.  Code that *needs* it could use it but most would not need to bother.

Python VMs would be free to have dummy do nothing implementations (ie: we should *never* guarantee that a python call to thing.__preallocate__(1000) followed immediately by a C extension module call that takes thing as input can assume anything about the underlying object... it needs to check the size or resize itself before using the unchecked _SET_ITEM macros, etc.)

----------
nosy: +gregory.p.smith

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue17338>
_______________________________________


More information about the Python-bugs-list mailing list