Ordered dict by default

Stephen Hansen apt.shansen at gmail.com
Thu Feb 5 11:01:35 EST 2009


> That page about Ruby dicts show a higher traversal speed (probably
> just because the CPU has to scan less memory, but I am not sure,
> because mordern CPUs are very complex) but lower insertion speed (I
> think mostly not because the added management of two pointers, but
> because the memory used increases, so there are more cache misses. If
> this turns out as true, then using the xor trick may halve the extra
> memory needed). I have no idea if such different balance of
> performance will lead to on average faster or slower Python programs,
> this has to be tested. But even if the average performance becomes a
> little worse I think making the default Python dict as ordered is a
> positive change for Python too, because built-ins are meant to be as
> flexible as possible, even if they aren't the fastest ones or the less
> memory hungry ones (and keeping dicts ordered decreases the surprises
> a newbie finds, makes some code cleaner, and doctests become simpler
> to write because the order of items may be more defined).

Er, doing a cursory review of my code has an overwhelmingly higher incidence
of insertions then traversals with my dictionaries: many dictionaries are
never traversed and when they are it is only once, and often had tens to
thousands of insertions before that traversal happens.

So a slight decrease in insertion speed at the cost of faster traversal seems
like a complete loss to me.

Now, I also do recognize the utility of ordered dictionaries in some cases, but
exactly what you mean by ordered varies. I have two cases where "ordered"
has the keys are in a specific custom order. I have four cases where "ordered"
means maintaining insertion order. For the former I do sorted(dict) on access
when I need to do something order-related. For the latter I have a simple
ordered dictionary implementation that maintains the keys as a separate list
attached to the dictionary.

But interestingly enough to me, in all the cases where I use an ordered dict
in my code, performance is completely irrelevant. Its tens to hundreds of
items usually, and the best optimization in the world wouldn't add more then
an imperceptible fraction of a second, and the memory lost to the extra
list adds up to nothing relevant.

I'm not claiming my use cases are everyones, not by any means. But: this
seems like a complete lose-lose for an idea from my perspective. Sure you
say an unordered dictionary could then be added for my needs, but... I like
my syntactic sugar, thank you :)

Ordered dictionaries can be useful, its good to see a standard implementation
going into the core, but the default? I hope not!

--Stephen



More information about the Python-list mailing list