[Python-Dev] Dictionary sparseness

Alex Martelli aleax@aleax.it
Mon, 5 May 2003 22:42:40 +0200


On Monday 05 May 2003 10:14 pm, Agthorr wrote:
> On Mon, May 05, 2003 at 03:34:58PM -0400, Tim Peters wrote:
> > Sure -- micro-optimizations are always fragile.  This kind of thing will
> > be done by someone who's certain the dict is henceforth read-only, and
> > who thinks it's worth the risk and obscurity to get some small speedup.
>
> An alternate optimization would be the additional of an immutable
> dictionary type to the language, initialized from a mutable dictionary

I'd love a read-only dictionary (AND a read-only list) for reasons having
little to do with optimization, actually -- ease of use as dict keys and/or
set members, plus, occasional help in catching errors (for the latter use
it would be wonderful if read-only dictionaries could be actually
substituted in place of such things as instance and class dictionaries).

Tuples are no substitutes for read-only lists because they lack many
useful "read-only" methods of lists (and won't grow them, as the BDFL
has abundantly made clear, as he sees tuples as drastically different
from lists).  Neither, even more clearly, are e.g. tuples of pairs a good
substitute for read-only dictionaries.

I've played with adding more selective "locking" to dicts but I was unable
to do it without a performance hit.  If wholesale "RO-ness" can in fact
*increase* performance in some cases, so much the better. "RO lists"
could probably save a little memory compared to normal ones since they
would need no "spare space" for growing.


Alex