I posted this as a proof-of-concept PR, so the tests would run : none failed (but at the same time none call the method, so…) (and actually it seems there may not be any test on the OrderedDict class ??).
There are some objections from @rhettinger on Github, I’ll answer them here.
This is a permanent API change just to optimize the idiom
for k in sorted(od): od.move_to_end(k).
Yes, more like the second one in that it’s in-place.
AFAICT people rarely use those idioms and when they do it is never speed critical; otherwise, they would be using a regular dict which already has much better performance than
I think there’s a chicken-and-egg situation here : people don’t use it because there’s no optimized way to do it - and also, in my opinion, because the for-move_to_end idiom itself is not obvious. Having an easily linkable OrderedDict.sort could change that a lot.
collections.OrderedDict is close to being a dead part of the collections module. Mostly It is only useful for being backwards compatible to versions before Python 3.6 and for creating LRU cache variants.
Lastly, it isn’t desirable to have API to diverge further from the regular dict.
I’ll merge those into one, because I don’t see why it wouldn’t be desirable to add things to OrderedDict per se, other than for keeping it “dead”.
Yes, the OrderedDict is mostly dead as it stands. Not as much as the UserX classes, since it does have the move_to_end method and the popitem(last=False) feature, but I agree it’s still quite comatose. I’m proposing we change that, and maybe not even in a big way. Why would changing its deadness be a bad thing at all ?
I understand the fear of adding a new feature which will have to be maintained, but not the link between that and the deadness of the class.
In a way I think this is indirectly asking why this should be done on OrderedDict rather than the builtin dict : primarily sorting is related to ordering, so it fits more the purpose of OrderedDict to have its ordering being changed, than the simple dict.
Furthermore, according to the thread I linked in the OP, there are performance and implementation issues with implementing a dict.sort method. On the opposite, this implementation is - as far as my benchmarks went - the quickest available, period, seemingly even better than creating a new dict by sorting the items. My intuition (for now) is that taking aventage of the doubly-linked list, while being a waste of memory, lets us gain time as compared to the builtin dict. The lock-during-sort feature isn’t a problem in any of the proposed implementations, as was the fear for a dict.sort method.