3 ms·
For what it's worth, the JS ecosystem has tried for years to get users to not rely on key iteration order. It was always unspecified but no matter what, users s
by munificent 10y ago
For what it's worth, the JS ecosystem has tried for years to get users to not rely on key iteration order. It was always unspecified but no matter what, users still expected them to iterate in insertion order.
V8 fought against it for years. Look at this bug horror show:
https://bugs.chromium.org/p/v8/issues/detail?id=164 https://bugs.chromium.org/p/v8/issues/detail?id=164
I think Python is doing the right thing. The only reason to not provide a deterministic iteration order for unsorted maps is to give more room for optimizers. But it seems like there is a clean implementation that is faster while also iterating over keys in insertion order.
Sure, maybe someone will come up with an even better optimization in the future that would break this, but at some point, you have to say, "OK, what we have is good enough, and it gives users a more predictable system."
If you are going to declare insertion order is non-deterministic, you really should make it non-deterministic by doing something like shuffling it. Otherwise, users will just inadvertently rely on whatever deterministic-but-unspecified-order the current implementation happens to provide.
- deleted 10y ago[deleted]
- xapata 10y ago> make it non-deterministic In Python v3.1 - v3.5 the order is deterministic within an execution, but non-deterministic across executions. This was implemented to avoid the possibility that a hacker could slow down dict key lookup/insertion by creating a large number of hash table collisions. https://mail.python.org/pipermail/python-announce-list/2012-March/009394.html https://mail.python.org/pipermail/python-announce-list/2012-... IMHO, that doesn't seem to be a big vulnerability.
- deleted 10y ago[deleted]
- wahern 10y agoIt never seemed to catch on with the bad guys, but that doesn't mean it wouldn't have happened if major languages hadn't implemented counter measures. DDoS using DNS amplification was possible for decades but never happened. Then one day a bad guy got bored, wrote a proof-of-concept, and the rest is history.
- danpat 10y agoFor once, C++ got some usability right and created std::map and std::unordered_map which have explicit key ordering behaviour. Now, back to waiting for my project to compile.....
- Sharlin 10y agoJava as well. Need sorted? TreeMap. High-performance but unordered? HashMap. Preserve insertion order? LinkedHashMap.
- tdb7893 10y agoI would guess it probably is the same way in c++ but I've never spent more than 5 seconds considering which collection to use in Java. They have a decent amount of them but once you are aware of their existence they are incredibly intuitive
- petertodd 10y agoFWIW Python has had OrderedDict for years.
- pekk 10y agoIf you think offering every variation of data structures is doing usability right, then C++ has done a lot of usability right. So much that its usability has suffered.