Skip to content

Add ODict to Base for OrderedDict #38163

Closed
@PallHaraldsson

Description

@PallHaraldsson

"unordered Dict iteration is a bug waiting to happen. Your PR would fix that."

My answer: https://discourse.julialang.org/t/replacement-for-dict-is-anyone-using-isslotfilled/48810/16?u=palli

I started that thread, asking if people would like Dict changed to ordered, but I realize by now that it's probably better to add the non-default ODict, so people can choose that, easily. At some point, e.g. Julia 2.0 we could change Dict to refer to ODict.

I neglected to mention the PR #38145, and one of the answers were:

"Generally, if you want something changed in Base and you want concrete feedback, it is best to make a PR"

elsewhere, people called for discussion. My PR already adds OrderedDict, and it works, it's just test failing because I'm replacing Dict, and with rather adding it as ODict, all the problems would go away.

@andyferris, I DO have some concerns, I thought I could choose any of the implementations, of ordered (not sorted) dicts available, but there's also this one I haven't looked at too closely (would it be better/ready?):

andyferris/Dictionaries.jl#31 (comment)

Midway through that I realised ordered dictionaries were a massive usability improvement, so that got incorporated here also.

The concrete implementation of Base.Dict is almost orthogonal in a sense - so long as we can merge it in a non-breaking way, it could enter Base at a different time (and Julia 1.6 makes a lot of sense to me, also).

Python added ordered dict to its standard library (despite "existing Python ordered-dict implementations") in 2008, with in rationale "PHP and Ruby 1.9 guarantee a certain order on iteration":

https://www.python.org/dev/peps/pep-0372/

Code ported from other programming languages such as PHP often depends on an ordered dict. Having an implementation of an ordering-preserving dictionary in the standard library could ease the transition and improve the compatibility of different libraries. [..]
Comparing two ordered dictionaries implies that the test will be order-sensitive [..] When ordered dicts are compared with other Mappings, their order insensitive comparison is used. This allows ordered dictionaries to be substituted anywhere regular dictionaries are used. [..]
Keeping a sorted list of keys is fast for all operations except delitem() which becomes an O(n) exercise. This data structure leads to very simple code and little wasted space.

Then for Python 3.7, in 2017-2018 the default dict became ordered by default:
https://mail.python.org/pipermail/python-dev/2017-December/151283.html

  • 50% less memory usage
  • 15% faster creation
  • 100% (2x) faster iteration
  • 20% slower move_to_end
  • 40% slower comparison

with changed implementation:

Remove doubly-linked list from C OrderedDict
https://bugs.python.org/issue31265#msg301942

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions