[Python-Dev] Guarantee ordered dict literals in v3.7?

Serhiy Storchaka storchaka at gmail.com
Mon Nov 6 02:44:12 EST 2017


06.11.17 05:01, INADA Naoki пише:
> FYI, Raymond's original compact dict (moving last item to slot used
> for deleted item) will break OrderedDict.  So it's not easy to implement
> than it looks.
> 
> OrderedDict uses linked list to keep which slot is used for the key.
> Moving last item will break it.
> It means odict.__delitem__ can't use PyDict_DelItem anymore and
> OrderedDict should touch internal structure of dict.

All this is implementation details. We did have little time for 
coordinated changing dict and OrderedDict implementations before 
releasing 3.6, and left existing coupling. This also prevents from 
in-place compactization of dict items.

We could add a private flag to dict that denotes whether this dict is 
ordered or no. The ordinal dict could be more compact, while OrderedDict 
left ordered. And I like your issue31265.

The current dict implementation still is young. It takes several 
optimizations in 3.7 (thank to you Inada) and AFAIK there are still not 
merged patches. I would wait until it become more stable before making 
change in language specification.

> I think current OrderedDict implementation is fragile loose coupling.
> While two object has different file (dictobject.c and odictobject.c),
> OrderedDict depends on dict's internal behavior heavily.
> It prevents optimizing dict. See comment here.
> 
> https://github.com/python/cpython/blob/a5293b4ff2c1b5446947b4986f98ecf5d52432d4/Objects/dictobject.c#L1082
> 
> I don't have strong opinion about what should we do about dict and OrderedDict.
> But I feel PyPy's approach (using same implementation and just override
> __eq__ and add move_to_end() method) is most simple.

I think that the coupling with dict and OrderedDict should be more 
robust, but left private. Dict implementation should be aware of 
OrderedDict and provide some private methods for using in OrderedDict, 
but not restrict the optimization of unordered dicts.



More information about the Python-Dev mailing list